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>

Linq to XML を使用して、この XML を読み取り、LearningObjective ノードの最大数をカウントし、LearningObjective ノードの数が Learning Objective ノードの最大数より少ない場合は、LearningObjectives ノードに空の LearningObjective ノードを書き戻したいと考えています。私はLinqを初めて使用します。この XML の変更について誰か助けてください。

4

1 に答える 1

0

さて、これを少し分割する必要があります。

  • XML のロード
  • LearningObjectivesすべてのコンテナ要素を見つける
  • LearningObjective各コンテナ内の要素をカウントする
  • それらのカウントの最大値を見つける
  • 必要に応じて空の要素を追加する

最初の 4 つは実際には非常に簡単です。

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

最後の 2 つのステートメントを組み合わせた可能性は十分にありますが、回答を明確にするためにそれらを分離したかったのです。

最後の部分として、実際には LINQ を使用しないことをお勧めします。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 に答える