Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

remove namespace from descendants

Question posted by: SR (Guest) on June 27th, 2008 07:20 PM
How can I remove an empty namespace with XElement ?
Using the below code I wish to apply the namespace only to the root node
<urlsetbut I obtain also an empty xmlns="" on the child nodes.

Thanks
Sandro

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XElement elRoot = new XElement(ns + "urlset",
new XElement ("url",
new XElement ("loc", "someurl...")
),
new XElement ("url",
new XElement ("loc", "someurl...")
)
);

XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
elRoot
);

doc.Save(Console.Out);
Console.ReadLine();

Result:

<?xml version="1.0" encoding="ibm850" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url xmlns="">
<loc>someurl...</loc>
</url>
<url xmlns="">
<loc>someurl...</loc>
</url>
</urlset>

Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: remove namespace from descendants
SR wrote:
Quote:
How can I remove an empty namespace with XElement ?
Using the below code I wish to apply the namespace only to the root node
<urlsetbut I obtain also an empty xmlns="" on the child nodes.


If you have
<foo xmlns="http://example.com/2008/ex1">
<bar>
<baz/>
</bar>
</foo>
then the namespace declaration on the foo element is in scope for the
descendants bar and baz too meaning if you want to create that snippet
with LINQ to XML you need
XNamespace ex1 = "http://goog-ajaxslt.sourceforge.net/";
XElement foo = new XElement(ex1 + "foo",
new XElement(ex1 + "bar",
new XElement(ex1 + "baz")));

Therefore if I understand you correctly then you want
Quote:
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XElement elRoot = new XElement(ns + "urlset",
new XElement ("url",

new XElement(ns + "url",
Quote:
new XElement ("loc", "someurl...")

new XElement(ns + "loc", ...)
Quote:
),
new XElement ("url",

new XElement(ns + "url",
Quote:
new XElement ("loc", "someurl...")

new XElement(ns + "loc", ...)

If that does not achieve what you are looking for the please post the
XML you want to create.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

SR's Avatar
SR
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: remove namespace from descendants

"Martin Honnen" <mahotrash@yahoo.dewrote in message
news:e5HTSCKxIHA.4560@TK2MSFTNGP03.phx.gbl...
Quote:
SR wrote:

Quote:
then the namespace declaration on the foo element is in scope for the
descendants bar and baz too meaning if you want to create that snippet
with LINQ to XML you need
XNamespace ex1 = "http://goog-ajaxslt.sourceforge.net/";
XElement foo = new XElement(ex1 + "foo",
new XElement(ex1 + "bar",
new XElement(ex1 + "baz")));

Quote:
If that does not achieve what you are looking for the please post the XML
you want to create.


Martin,
that was exactly what I am looking for..
Thanks a lot

Greetings from Italy
Sandro


 
Not the answer you were looking for? Post your question . . .
189,798 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors