473,484 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

remove namespace from descendants

SR
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>

Jun 27 '08 #1
2 8175
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.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
XNamespace ns = "http://www.sitemaps.org/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
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
SR

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:e5**************@TK2MSFTNGP03.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.sourceforge.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

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5305
by: Ender | last post by:
I am having an issue trying to use the SelectNodes method in C# when using an Xpath Query. I realize lots of people have posted on this subject, but I have a little bit of a different variation. ...
6
3493
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
2
11818
by: Joe Bloggs | last post by:
Hi, I am trying to remove the namespace in the root node in the following XML, <ordersHistory xmlns="http://tempuri.org/Orders.xsd"> <order number="PO-1" added="12/12/2002"> <client...
6
24444
by: fzhang | last post by:
I am relatively new to XML and C#. So, forgive me if this question is too newbie. :-) While assuming this is an easy programming task, I couldn't find a single reference anywhere for how to do...
6
18950
by: pavel.repkin | last post by:
Hey! How would you do the following task? Let you have an XML tree on input. Suppose, there is a special kind of node you want to remove. Let it have "bad" name. Each "bad" node has a parent...
1
4368
by: dignan.tenenbaum | last post by:
Hello, I'm using the XmlReader.ReadOuterXml() method to return the string representation of an xml node. The XmlReader is created with a file path and a XmlReaderSettings object. This was...
0
537
by: SR | last post by:
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...
0
884
by: George Johnston | last post by:
Put your namespace inside of curly brackets. string ns = "{" + xd.Root.Name.NamespaceName + "}"; IEnumerable<XElementd = xd.Descendants(ns + "People").Descendants();
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7137
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7194
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3038
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
587
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.