如何从出现在多个嵌套级别的 XMLDocument 中删除 XMLNode
How to remove a XMLNode from XMLDocument occuring at multiple nested levels
我有一个 XML,它有一个节点,该节点使用 C# 在文件中的多个级别重复。
XML 示例:
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
我想从此 XML 文档中删除所有 isbn 节点,无论其位置如何。
最简单的方法是使用 XDocument 而不是 XmlDocument。使用 .Descendants() 查找特定 name/type 的所有节点。然后 .Remove() 它们。
string xml = @"<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>";
XDocument xdoc = XDocument.Parse(xml);
xdoc.Descendants("isbn").Remove();
string result = xdoc.ToString();
但是如果您想使用 XmlDocument,请使用此代码:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
foreach (var node in new List<XmlNode>(xmldoc.GetElementsByTagName("isbn")
.OfType<XmlNode>()).Where(
x => x.ParentNode != null))
{
node.ParentNode.RemoveChild(node);
}
string result = xmldoc.OuterXml;
正如您指出的那样,您正在父节点上使用 XmlDocument
, you need to remove a child XmlElement
node via the RemoveChild
方法:
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
";
// Initialize and load the XmlDocument
var doc = new XmlDocument();
doc.LoadXml(xml);
// Delete all XmlElements named "isbn".
var list = doc.DocumentElement.GetElementsByTagName("isbn").OfType<XmlElement>().ToArray();
foreach (var element in list)
{
var parent = element.ParentNode;
if (parent != null)
parent.RemoveChild(element);
}
var newXml = doc.OuterXml;
Debug.WriteLine(newXml);
输出为:
<?xml version="1.0" encoding="utf-16"?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
</book>
<book>
<title>The Walking Dead</title>
<author>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
我有一个 XML,它有一个节点,该节点使用 C# 在文件中的多个级别重复。
XML 示例:
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
我想从此 XML 文档中删除所有 isbn 节点,无论其位置如何。
最简单的方法是使用 XDocument 而不是 XmlDocument。使用 .Descendants() 查找特定 name/type 的所有节点。然后 .Remove() 它们。
string xml = @"<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>";
XDocument xdoc = XDocument.Parse(xml);
xdoc.Descendants("isbn").Remove();
string result = xdoc.ToString();
但是如果您想使用 XmlDocument,请使用此代码:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
foreach (var node in new List<XmlNode>(xmldoc.GetElementsByTagName("isbn")
.OfType<XmlNode>()).Where(
x => x.ParentNode != null))
{
node.ParentNode.RemoveChild(node);
}
string result = xmldoc.OuterXml;
正如您指出的那样,您正在父节点上使用 XmlDocument
, you need to remove a child XmlElement
node via the RemoveChild
方法:
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
";
// Initialize and load the XmlDocument
var doc = new XmlDocument();
doc.LoadXml(xml);
// Delete all XmlElements named "isbn".
var list = doc.DocumentElement.GetElementsByTagName("isbn").OfType<XmlElement>().ToArray();
foreach (var element in list)
{
var parent = element.ParentNode;
if (parent != null)
parent.RemoveChild(element);
}
var newXml = doc.OuterXml;
Debug.WriteLine(newXml);
输出为:
<?xml version="1.0" encoding="utf-16"?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
</book>
<book>
<title>The Walking Dead</title>
<author>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>