473,609 Members | 2,222 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 8190
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
5312
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
3512
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
11826
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
24480
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 xmlns="the-namespace">
6
18967
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 rule is applied to all the ancetors of the "bad" node
1
4378
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 namespace declarations to the string it returned. xml example ...
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
897
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
8115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8557
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6047
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5504
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4007
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1374
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.