Monday, 15 September 2014

c# - How to specify an xmlns for XDocument? -



c# - How to specify an xmlns for XDocument? -

i tried:

textbox1.text = new xdocument(new xdeclaration("1.0", "utf-8", "yes"), new xelement("root1", new xattribute( "xmlns", @"http://example.com"), new xelement("a", "b")) ).tostring();

but get:

the prefix '' cannot redefined '' 'http://example.com' within same start element tag.

i tried substituting (according reply found) :

xattribute(xnamespace.xmlns,...

but got error well.

note: i'm not trying have more 1 xmlns in document.

the way xdocument api works namespace-scoped names xname instances. these easy work with, long take xml name isn't string, scoped identifier. here's how it:

var ns = xnamespace.get("http://example.com"); var doc = new xdocument(new xdeclaration("1.0", "utf-8", null)); var root = new xelement(ns + "root1", new xelement(ns + "a", "b")); doc.add(root);

result:

<root1 xmlns="http://example.com"> <a>b</a> </root1>

note + operator overloaded take xnamespace , string result in , xname instance.

c# .net xml xml-namespaces linq-to-xml

No comments:

Post a Comment