473,548 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ 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.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>

Jun 27 '08 #1
2 8185
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
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
SR

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

Jun 27 '08 #3

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

Similar topics

4
5310
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. Let's take the document <MyNodes> <MyNode Attrib1="Cat"/> <MyNode Attrib1="Dog"/> <MyNode OtherAttrib="House"/>
6
3507
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" and "ns0:Company" but I do not want to delete their child nodes("w:p","w:r","w:t"). How can I do this? <ns0:Proposal> <ns0:Company> <w:p> <w:r>
2
11822
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 name="Microsoft"> <address country="USA" city="Washington" /> <notes> Produces a great framework for developing applications.
6
24465
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 it. Here is the situation: I am given an XML file like the one below from other group in my company to load the data into our database. <root...
6
18961
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 node, obviously. In case there are no children left in the parent after removal of all the "bad" nodes, the parent must also be removed. And this...
1
4374
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 working fine until a default namespace was introduced. When the default namespace is defined the ReadOuterXml() method began adding a bunch of extra...
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 Sandro XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; XElement elRoot = new XElement(ns + "urlset", new XElement ("url",
0
892
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
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7954
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
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 we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.