From: Ilyas on
Hi all

I have the following code:

Namespace ns = "http://www.abc.com";
XDocument xDoc = new XDocument(
new XElement(ns + "root",
new XElement("person",
new XAttribute("id", 1),
new XElement("forename", "jack"),
new XElement("age", 35)),
new XElement("person",
new XAttribute("id", 2),
new XElement("forename", "bob"),
new XElement("age", 2)
)));

This is the xml written if I do Console.WriteLine(xDoc):
<root xmlns="http://www.abc.com">
<person id="1" xmlns="">
<forename>jack</forename>
<age>35</age>
</person>
<person id="2" xmlns="">
<forename>bob</forename>
<age>2</age>
</person>
</root>

However why do both person elements have a blank for xmlns. I would
have though that would have not been there....can anyone explain. How
do I get rid of the xmlns='' ?

What I want is if no namespace is specified then my xml elements
should be in the default namespace (http://www.abc.com) - how can
Imodify the above code so I dont end up with xmlns='' in the person
tags...?

Thanks
From: Martin Honnen on
Ilyas wrote:

> What I want is if no namespace is specified then my xml elements
> should be in the default namespace (http://www.abc.com) - how can
> Imodify the above code so I dont end up with xmlns='' in the person
> tags...?

When you create XML with namespace programmatically the namespace of
each element or attribute node is determined when you create it. If you
want an element "foo" to be in a certain namespace (stored in variable
ns) you need to use e.g.
new XElement(ns + "foo")
each time you create an element. So you need

Namespace ns = "http://www.abc.com";
XDocument xDoc = new XDocument(
new XElement(ns + "root",
new XElement(ns + "person",
new XAttribute("id", 1),
new XElement(ns + "forename", "jack"),
new XElement(ns + "age", 35)),
new XElement(ns + "person",
new XAttribute("id", 2),
new XElement(ns + "forename", "bob"),
new XElement(ns + "age", 2)
)));

If you only do
new XElement("foo")
then you create an element with local name "foo" in _no_ namespace and
that way the serialization needs to add xmlns="" if an ancestor is in a
namespace.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
From: Ilyas on
On Feb 25, 2:27 pm, Martin Honnen <mahotr...(a)yahoo.de> wrote:
> Ilyas wrote:
> > What I want is if no namespace is specified then my xml elements
> > should be in the default namespace (http://www.abc.com) - how can
> > Imodify the above code so I dont end up with xmlns='' in the person
> > tags...?
>
> When you create XML with namespace programmatically the namespace of
> each element or attribute node is determined when you create it. If you
> want an element "foo" to be in a certain namespace (stored in variable
> ns) you need to use e.g.
>    new XElement(ns + "foo")
> each time you create an element. So you need
>
> Namespace ns = "http://www.abc.com";
>              XDocument xDoc = new XDocument(
>                  new XElement(ns + "root",
>                      new XElement(ns + "person",
>                          new XAttribute("id", 1),
>                          new XElement(ns + "forename", "jack"),
>                          new XElement(ns + "age", 35)),
>                      new XElement(ns + "person",
>                          new XAttribute("id", 2),
>                          new XElement(ns + "forename", "bob"),
>                          new XElement(ns + "age", 2)
>                      )));
>
> If you only do
>    new XElement("foo")
> then you create an element with local name "foo" in _no_ namespace and
> that way the serialization needs to add xmlns="" if an ancestor is in a
> namespace.
>
> --
>
>         Martin Honnen --- MVP XML
>        http://msmvps.com/blogs/martin_honnen/

Okay that makes sense what youre saying - My thought was that If I
dont specify a namespace, then my element would be in the default
namespace (in this case http://www.abc.com), but that doesnt seem to
be the case.

It makes the code look a bit ugly with all this namespace
concatenation too - is there anyway I can get around that, perhaps by
using a different method/approch altogether?

From: Martin Honnen on
Ilyas wrote:

> Okay that makes sense what youre saying - My thought was that If I
> dont specify a namespace, then my element would be in the default
> namespace (in this case http://www.abc.com), but that doesnt seem to
> be the case.

You have to distinguish between writing or authoring XML and creating it
programmatically. If you write/author

<root xmlns="http://example.com/">
<foo>
<bar/>
</foo>
</root>

then the default namespace declaration on the 'root' element applies to
its descendant elements ('foo', 'bar') as well.

However when you create elements programmatically they get their
namespace when you create them and don't change them or adapt them
depending on any parent or ancestor you add them to.


> It makes the code look a bit ugly with all this namespace
> concatenation too - is there anyway I can get around that, perhaps by
> using a different method/approch altogether?

The LINQ to XML API does not have anything as far as I know, you would
have to write your own. With VB.NET you can use XML literals and define
a namespace at the beginning of your module with e.g.
Imports <xmlns="http://example.com/">


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 | 
Pages: 1
Prev: Timer in class?
Next: siaqodb