【问题标题】:Changing XML with Linq使用 Linq 更改 XML
【发布时间】:2013-07-11 12:59:48
【问题描述】:

我编写了一个 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。

【问题讨论】:

  • 到目前为止你有什么(除了这些要求)? LearningObjective 节点的maximum 是什么意思?

标签: xml linq xpath


【解决方案1】:

好的,所以我们应该分开一下:

  • 加载 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"));
    }
}

【讨论】:

  • 谢谢乔恩。我试过你的步骤。它给出了我提到的几乎相同的结果。如果我想将属性 ActionFieldId 设置为空 LearningObjective,我需要获取所有 ActionFieldIds 并找到当前 LearningObjectives 缺少任何 Id,然后使用缺少的 ActionFieldId 创建 LearningObjective 节点?
  • @user2331747:我真的不明白你的意思,老实说......你没有提到任何关于在问题中设置属性的内容。
  • 对不起,我以为 LearningObjective 不需要设置属性。但在前端,我们确实需要为空的 LearningObjective 节点提供该属性。我的意思是有这样的空节点:
  • @user2331747:好的 - 所以也许你只需要 new XAttribute("ActionFieldId", i + 1) 作为 XElement 构造函数的第二个参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 2010-12-02
相关资源
最近更新 更多