删除NODE保留孩子C# [英] Remove NODE preserving childs C#

查看:100
本文介绍了删除NODE保留孩子C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的xml:

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);

推荐答案

Xml构造不良,代码示例使用未定义的类,没有提到你在哪一行代码中遇到了什么问题。



这里修复了XML:

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>



以下是使用上述固定xml的解决方案:


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>


这篇关于删除NODE保留孩子C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆