Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have a xml with this format:
<a:label>
<b:title>
	<b:detail>
		<b:names>
			<b:name>the name
			<b:colour>blue
		
		<b:attib>
			<b:size>A4






I want to remove the label and title, but preserving the remaining part.
How can i do it?

What I have tried:

I tried:
C#
XDocument input = XDocument.Parse(theRow["xml"].ToString());
XElement output = input.Root.Elements().First();

XmlNode eliminoxml2 = cfexml.SelectSingleNode("//b:title", spacemanager);
eliminoxml2.RemoveChild(eliminoxml2);
Posted
Updated 10-Nov-17 17:34pm
v2
Comments
phil.o 10-Nov-17 16:26pm    
Without seeing the closing tags, it will be hard to guess the corresponding hierarchy. Please show a valid xml, what you have shown would not validate.
j snooze 10-Nov-17 17:24pm    
Not knowing if you have multiple title nodes or not, why not just do a xpath query of selectnodes("//b:title") and throw the childnodes in their own nodes. then you can just loop through and write those out or whatever you are trying to do.

1 solution

The Xml is poorly formed, the code sample uses classes not defined, and there is no mention of what issues that you are having with which line of code.

Here is the XML fixed:
XML
<?xml version="1.0" encoding="UTF-8"?>
<a:label
	xmlns:a="http://www.myurl.com/a">
	<b:title
		xmlns:b="http://www.myurl.com/b">
		<b:detail>
			<b:names>
				<b:name>the name</b:name>
				<b:colour>blue</b:colour>
			</b:names>
			<b:attib>
				<b:size>A4</b:size>
			</b:attib>
		</b:detail>
	</b:title>
</a:label>

Here is a solution using the above fixed xml:
C#
var rawXml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
               <a:label xmlns:a=""http://www.myurl.com/a"">
                    <b:title xmlns:b=""http://www.myurl.com/b"">
                        <b:detail>
			                <b:names>
				                <b:name>the name</b:name>
				                <b:colour>blue</b:colour>
			                </b:names>
			                <b:attib>
				                <b:size>A4</b:size>
			                </b:attib>
		                </b:detail>
	                </b:title>
                </a:label>";

var input = XDocument.Parse(rawXml);

var output = input.Root.Elements().Elements().First().ToString();
Console.WriteLine(output);

Which outputs:
XML
<b:detail xmlns:b="http://www.myurl.com/b">
  <b:names>
    <b:name>the name</b:name>
    <b:colour>blue</b:colour>
  </b:names>
  <b:attib>
    <b:size>A4</b:size>
  </b:attib>
</b:detail>
 
Share this answer
 
v2
Comments
BillWoodruff 12-Nov-17 19:22pm    
+5 for use of psychic powers :)
Graeme_Grant 12-Nov-17 20:15pm    
LMAO... Thanks Bill ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900