修改xml将子节点合并为单个节点
Modify xml to merge child nodes to a single node
我有 XML 来自服务的响应。
var Response = httpService.GetResponse();
XDocument doc = XDocument.Parse(Response);
xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
</Parent>
<Parent>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</edmx:Edmx>
我如何编辑它使其看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</edmx:Edmx>
一旦您了解了如何使用 XDocument、XElement,这就非常简单了。
我刚刚创建了一个 List<XElement>
来收集所有子元素,然后我将它们附加到一个新的父元素并将父元素分配给根元素。
我强烈建议创建一个新文档而不是更新现有文档。
解决方案 1:详细版本以了解其工作原理。
XDocument xDoc = XDocument.Parse(str);
List<XElement> allChildNodes = new List<XElement>();
foreach (var parent in xDoc.Root.Elements("Parent"))
{
allChildNodes.AddRange(parent.Descendants());
}
XElement xParent = new XElement("Parent");
xParent.Add(allChildNodes);
xDoc.Root.Descendants().Remove();
xDoc.Root.Add(xParent);
解决方案 2:感谢 Jeff Mercado,我们有一个紧凑的版本。
XDocument xDoc = XDocument.Parse(str);
xDoc.Root.ReplaceNodes(
new XElement("Parent", // New parent element is created
xDoc.Root.Elements("Parent").Elements()));
输出:
<root>
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</root>
要替换 Parent
节点,您可以使用 XElement.ReplaceNodes()
替换根的子节点。用包含被替换节点的子节点的新 Parent
节点替换这些节点。
doc.Root.ReplaceNodes(
new XElement("Parent",
doc.Root.Elements("Parent").Elements()
)
);
我有 XML 来自服务的响应。
var Response = httpService.GetResponse();
XDocument doc = XDocument.Parse(Response);
xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
</Parent>
<Parent>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</edmx:Edmx>
我如何编辑它使其看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</edmx:Edmx>
一旦您了解了如何使用 XDocument、XElement,这就非常简单了。
我刚刚创建了一个 List<XElement>
来收集所有子元素,然后我将它们附加到一个新的父元素并将父元素分配给根元素。
我强烈建议创建一个新文档而不是更新现有文档。
解决方案 1:详细版本以了解其工作原理。
XDocument xDoc = XDocument.Parse(str);
List<XElement> allChildNodes = new List<XElement>();
foreach (var parent in xDoc.Root.Elements("Parent"))
{
allChildNodes.AddRange(parent.Descendants());
}
XElement xParent = new XElement("Parent");
xParent.Add(allChildNodes);
xDoc.Root.Descendants().Remove();
xDoc.Root.Add(xParent);
解决方案 2:感谢 Jeff Mercado,我们有一个紧凑的版本。
XDocument xDoc = XDocument.Parse(str);
xDoc.Root.ReplaceNodes(
new XElement("Parent", // New parent element is created
xDoc.Root.Elements("Parent").Elements()));
输出:
<root>
<Parent>
<ChildType1>contentA</ChildType1>
<ChildType2>contentB</ChildType2>
<ChildType3>contentC</ChildType3>
<ChildType1>contentD</ChildType1>
<ChildType3>contentE</ChildType3>
</Parent>
</root>
要替换 Parent
节点,您可以使用 XElement.ReplaceNodes()
替换根的子节点。用包含被替换节点的子节点的新 Parent
节点替换这些节点。
doc.Root.ReplaceNodes(
new XElement("Parent",
doc.Root.Elements("Parent").Elements()
)
);