Click here to Skip to main content
13,256,123 members (41,726 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
XDocument input = XDocument.Parse(theRow["xml"].ToString());
XElement output = input.Root.Elements().First();
 
XmlNode eliminoxml2 = cfexml.SelectSingleNode("//b:title", spacemanager);
eliminoxml2.RemoveChild(eliminoxml2);
Posted 10-Nov-17 10:58am
Updated 10-Nov-17 18: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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 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:
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:
<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>
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 10 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100