2

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 ns = XNamespace.Get("urn:APISchema.xsd");
 root = new XElement(ns + "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...?

4 Answers 4

3
root.Add(new XElement(namespace + "foo", "str"))

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

Sign up to request clarification or add additional context in comments.

4 Comments

BTW, "Namespace" is a bad variable name :)
@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.
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. stackoverflow.com/questions/477962/…
this seems odd to me that i need to add namespace to every bit and piece... anyway thanks.
0

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").

1 Comment

i just want to add namespace to root element.. could you please provide a little more detail.
0
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.

2 Comments

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...
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.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.