tags:

views:

1479

answers:

4

i am creating xml using linq.xml through xelement. my hirerachy is some thing like this

I want this schema 2 str

here is my code for schema generation

 XNamespace Namespace = XNamespace.Get("urn:APISchema.xsd");
 root = new XElement(namepsace + "Foo");
 root.Add(new XElement("version", "2"));
 root.Add(new XElement("foochild", "str"));

but the resultant schema is

<Foo xlmns="urn:APISchema.xsd">
<version xlmns="">2</version>
<foochild xlmns="">str</foochild>
</Foo>

any idea why such problem why it is appending xlmn to root childs...?

+1  A: 
root.Add(new XElement(namespace + "foo", "str"))

Edit: upon further SO searching, this question seems to be addressing the same issue.

Richard Morgan
BTW, "Namespace" is a bad variable name :)
Richard Morgan
@root.Add(new XElement(namespace + "foo", "str"))no its not producing required result.... as my xml is far more long..regarding namespace yep! i agree its bad variable name :) but its just typo for code sample above.
Usman Masood
Every element has a namespace, so when you don't use XNamespace in front of the element name, the XElement constructor assumes you mean to override the parent namespace with "". In short, you need to add the namespace to everything... looking to the right in the SO Related box, this link might help. http://stackoverflow.com/questions/477962/how-to-create-xelement-with-default-namespace-for-children-without-using-xnamespa
Richard Morgan
this seems odd to me that i need to add namespace to every bit and piece... anyway thanks.
Usman Masood
A: 

You added to the element 'usr:APISchema.xsd::Foo' two elements w/o a namespace. The resulted XML is the expected one. You must add the namespace to each added element: root.Add(new XElement(namespace + "foochild").

Remus Rusanu
i just want to add namespace to root element..could you please provide a little more detail.
Usman Masood
A: 
XNamespace myNameSpace = XNamespace.Get("urn:APISchema.xsd");
        root = new XElement(myNameSpace + "Foo",
                                new XElement(myNameSpace + "foo", "str"));

IMO This is easier to read. But as Richard stated you just need to add the namespace.

David Yancey
David... the sample xml i gave is just for example... my actual xml is containing lot more nested elements and its kinda dynamic.. so i can't use this...what i am doig is creating a root element with namespace and than adding elements to it...the only problem is its displaying xmlns="" to root level childs too which i don't want...
Usman Masood
How are you buidling your dynamic XML? You do have to add the namespace to each of the child nodes as you have done in your root, if you could post a sample of how your building the dynamic it might help.
David Yancey
A: 
XNamespace myNamespace = XNamespace.Get("urn:APISchema.xsd");
root = new XElement(myNamespace + "Foo",
    new XElement(myNamespace + "version", "2"),
    new XElement(myNamespace + "foochild", "str"));

Give that a shot, it should do the trick for you.

Alexander Kahoun