0

我编写了一个 XML,如下所示:

<ArrayOfProductLine>
    <ProductLine>
        <Name>CRM</Name>
        <ActionFields>
            <ActionField Id="1">
                <Name>A2</Name>
            </ActionField>
            <ActionField Id="2">
                <Name>A1</Name>
            </ActionField>
        </ActionFields>
        <ProcessSteps>
            <ProcessStep>
                <Name>Marketing</Name>
                <LearningObjectives>
                    <LearningObjective ActionFieldId="1">
                        <Paragraphs>
                            <Paragraph AllowSelection="false">
                                <Text>Lern Ziel2</Text>
                                <Id>1</Id>
                            </Paragraph>
                            <Paragraph AllowSelection="false">
                                <Text>test</Text>
                                <Id>4</Id>
                            </Paragraph>
                        </Paragraphs>
                    </LearningObjective>
                    <LearningObjective ActionFieldId="2">
                        <Paragraphs>
                            <Paragraph AllowSelection="false">
                                <Text>Lern Ziel2.1</Text>
                                <Id>2</Id>
                            </Paragraph>
                        </Paragraphs>
                    </LearningObjective>
                </LearningObjectives>
            </ProcessStep>
            <ProcessStep>
                <Name>Vertrieb</Name>
                <LearningObjectives>
                    <LearningObjective ActionFieldId="1">
                        <Paragraphs>
                            <Paragraph AllowSelection="false">
                                <Id>3</Id>
                            </Paragraph>
                        </Paragraphs>
                    </LearningObjective>
                </LearningObjectives>
            </ProcessStep>
        </ProcessSteps>
    </ProductLine>
</ArrayOfProductLine>

如果 LearningObjective 节点的数量小于使用 Linq to XML 的最大学习目标节点数,我想读取此 XML 并计算 LearningObjective 节点的最大值并在 LearningObjectives 节点中写回空的 LearningObjective 节点。我对 Linq 很陌生。任何人都可以帮助我更改此 XML。

4

1 回答 1

0

好的,所以我们应该把它分开一点:

  • 加载 XML
  • 查找所有LearningObjectives容器元素
  • 计算LearningObjective每个容器中的元素
  • 找到这些计数的最大值
  • 必要时添加空元素

前四个实际上非常简单:

XDocument doc = XDocument.Load("data.xml");
var containers = doc.Descendants("LearningObjectives").ToList();
var containerCounts = containers.Select(x => x.Elements("LearningObjectives")
                                              .Count());
var maxCount = containerCounts.Max();

我很可能会合并最后两个陈述,但我想将它们分开以使答案清晰。

对于最后一部分,我建议不要实际使用 LINQ——它不适合突变。我们需要做的就是查看所有容器,并为每个容器添加正确数量的元素:

foreach (var container in containers)
{
    // Same approach to counting as before.
    int count = x => x.Elements("LearningObjectives").Count()
    for (int i = count; i < maxCount; i++)
    {
        container.Add(new XElement("LearningObjective"));
    }
}
于 2013-07-11T13:07:29.680 回答