remove namespace from descendants

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SR

    remove namespace from descendants

    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.or g/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(Consol e.Out);
    Console.ReadLin e();

    Result:

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

  • Martin Honnen

    #2
    Re: remove namespace from descendants

    SR wrote:
    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.sourcef orge.net/";
    XElement foo = new XElement(ex1 + "foo",
    new XElement(ex1 + "bar",
    new XElement(ex1 + "baz")));

    Therefore if I understand you correctly then you want
    XNamespace ns = "http://www.sitemaps.or g/schemas/sitemap/0.9";
    XElement elRoot = new XElement(ns + "urlset",
    new XElement ("url",
    new XElement(ns + "url",
    new XElement ("loc", "someurl... ")
    new XElement(ns + "loc", ...)
    ),
    new XElement ("url",
    new XElement(ns + "url",
    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

    Comment

    • SR

      #3
      Re: remove namespace from descendants


      "Martin Honnen" <mahotrash@yaho o.dewrote in message
      news:e5HTSCKxIH A.4560@TK2MSFTN GP03.phx.gbl...
      SR wrote:
      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.sourcef orge.net/";
      XElement foo = new XElement(ex1 + "foo",
      new XElement(ex1 + "bar",
      new XElement(ex1 + "baz")));
      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

      Comment

      Working...