以下是xml文档
<al>
<hfs>
<hf id="1">A2</hf> ##1) based these hf nodes id attributes
<hf id="2">A1</hf>
</hfs>
<psteps>
<pstep>
<name>sharepoint</name>
<lze>
<lz hid="1"> ##1) in Lze node need to create same number lz nodes
## based previously quoted hf node id attribute
<ps>
<p>
<text>ziel</text>
<inhalt>ttt</inhalt>
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
<lz hid="2">
<ps>
<p>
<text></text>
<inhalt></inhalt> ##2) lz node with hid = "2" has 2 p nodes
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
</lze>
</pstep>
<pstep>
<name>aspnet</name>
<lze>
<lz hid="2"> ## 1)in Lze node need to create same number lz nodes
## based previously quoted hf node id attribute
<ps>
<p>
<text>ziel</text> ## 2)lz node with hid = "2" has 1 p node so need
<inhalt>ttt</inhalt> ## node to create 1 more as previously
</p> ## lz node
</ps>
</lz>
</lze>
</pstep>
</psteps>
</al>
我需要在这里做两件事:
基于hfs子hf节点我需要在hfs兄弟psteps后代lze节点中创建相同数量的具有相同属性值的lz节点
接下来必须在具有相同属性值的 lz 节点中找到最大数量的 p 节点,并在具有相同属性值的其余 lz 节点中创建相同数量的 p 节点。
这是我正在尝试生成的示例 xml:
<al>
<hfs>
<hf id="1">A2</hf>
<hf id="2">A1</hf>
</hfs>
<psteps>
<pstep>
<name>sharepoint</name>
<lze>
<lz hid="1">
<ps>
<p>
<text>ziel</text>
<inhalt>ttt</inhalt>
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
<lz hid="2">
<ps>
<p>
<text></text>
<inhalt></inhalt>
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
</lze>
</pstep>
<pstep>
<name>aspnet</name>
<lze>
<lz hid="2">
<ps>
<p>
<text>ziel</text>
<inhalt>ttt</inhalt>
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
<lz hid="1">
<ps>
<p>
<text></text>
<inhalt></inhalt>
</p>
<p>
<text></text>
<inhalt></inhalt>
</p>
</ps>
</lz>
</lze>
</pstep>
</psteps>
<al>
我使用以下代码能够创建<lz>
缺少节点的节点。我需要创建<p>
节点,我无法<p>
从每个节点中选择<lz>
节点。
var doc = XDocument.Load("XmlFile1.xml");
var hfIds = (from hf in doc.Descendants("hf")
from attr in hf.Attributes("Id")
select attr.Value).Distinct(StringComparer.Ordinal).ToList();
var lze2 = doc.Descendants("lze")
.Select(lze => new {
element = lze,
hfids = lze.Descendants("lz").Attributes("hid"),
paras = (from lz in lze.Elements("lz")
from ps in lz.Elements("ps")
from p in ps.Elements("p")
select p).ToList()
});
foreach (var c in lze2) {
foreach (var hfid in hfIds.Where(hfid => !c.hfids.Any(attr => hfid.Equals(attr.Value)))) {
c.element.Add(new XElement("lz", new XAttribute("hfid", hfid),
new XElement("ps",
new XElement("p"),
new XElement("Text"),
new XElement("Content"))));
break;
}
}
谢谢您的帮助