c# - Linq 2 XML - 如何读取具有相同名称的元素

发布于 2024-10-20 15:28:41 字数 720 浏览 3 评论 0原文

xml的结构是:

<Rules>
    <rule ID="1" Parameter="IsD">
      <body>
        <ID>2</ID>
        <Ratio>=</Ratio>
        <Value>no</Value>
        <Operation>AND</Operation>
        <ID>3</IF_ID>
        <Ratio>=</Ratio>
        <Value>yes</Value>
      </body>
      <value>no problems</value>
    </rule>
</Rules>

如何读取元素并创建具有以下结构的规则:

If (<ID><Ratio><Value><Operation><ID><Ratio><Value>)

ID是问题ID。就我而言:

If (2 == no && 3 == yes)

请帮助我。

the stucture of xml is:

<Rules>
    <rule ID="1" Parameter="IsD">
      <body>
        <ID>2</ID>
        <Ratio>=</Ratio>
        <Value>no</Value>
        <Operation>AND</Operation>
        <ID>3</IF_ID>
        <Ratio>=</Ratio>
        <Value>yes</Value>
      </body>
      <value>no problems</value>
    </rule>
</Rules>

How can I read elements and create a rule with the following structure:

If (<ID><Ratio><Value><Operation><ID><Ratio><Value>)

ID is a question ID. In my case:

If (2 == no && 3 == yes)

Please, help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-10-27 15:28:41

您可以使用 XElement 类的 Elements 方法。例如(假设您循环遍历所有 rule),您可以编写

foreach (var rule in document.Root.Descendants("rule")) {
  var body = rule.Element("body");
  for (var i=0; i<body.Elements("ID").Count) {
    var id = body.Elemets("ID").ElementAt(i);
    var ratio = body.Elements("Ratio").ElementAt(i);
    var value = body.Elements("Value").ElementAt(i);
    /// ...
  }
}

Notet,通过使用 ElementAt()Elements 枚举的元素> 可能不是很有效,另一种方法是使用单独的 foreach 循环迭代不同的子元素并将它们存储在 List 中(或者更好,使用 ToList 扩展方法)。

要点是,Elements 枚举按文档顺序返回子元素,即按照它们在输入文档中出现的顺序(这就是您在这里需要的)。

更好的解决方案是(IMO)在输入文档中添加更多结构以反映逻辑,例如:

<Rules>
  <rule ID="1" Parameter="IsD">
    <body>
      <term >
        <ID>2</ID>
        <Ratio>=</Ratio>
        <Value>no</Value>
      </term>
      <term operation="and">
        <ID>3</IF_ID>
        <Ratio>=</Ratio>
        <Value>yes</Value>
      </term>
  </body>
  <value>no problems</value>
</rule>

You could use the Elements method of the XElement class. For instance (supposing that you loop over all rules) you could write

foreach (var rule in document.Root.Descendants("rule")) {
  var body = rule.Element("body");
  for (var i=0; i<body.Elements("ID").Count) {
    var id = body.Elemets("ID").ElementAt(i);
    var ratio = body.Elements("Ratio").ElementAt(i);
    var value = body.Elements("Value").ElementAt(i);
    /// ...
  }
}

Notet that accessing the elements of the Elements enumeration by use of ElementAt() might not be very efficient, another approach would be to iterate over the different subelements with separate foreach loops and store them in a List (or better, use the ToList Extension method).

The point is, that the Elements enumeration returns the subelements in document order i.e. in the same order as they appear in the input document (which is, what you need here).

A better solution would be (IMO) to put more structure in your input document in order to reflect the logic, like:

<Rules>
  <rule ID="1" Parameter="IsD">
    <body>
      <term >
        <ID>2</ID>
        <Ratio>=</Ratio>
        <Value>no</Value>
      </term>
      <term operation="and">
        <ID>3</IF_ID>
        <Ratio>=</Ratio>
        <Value>yes</Value>
      </term>
  </body>
  <value>no problems</value>
</rule>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文