使用 LINQ 读取和有条件地写入复杂的 XML 文件

发布于 2024-12-15 13:43:17 字数 2288 浏览 0 评论 0原文

我怀疑使用 LINQ to XML 可以非常轻松地解决我的问题,但我对它相当陌生,似乎遇到了困难。 (另外,我找不到一个像样的教程来涵盖与我类似的案例,所以如果你能指出我那里的任何内容,我将非常感激!)

我需要做的是:

  • 我有一个 XML我必须读取的文件,您可以在下面找到该文件。文件中通常只有一个分支,其中包含多个订单。每个订单都有一个唯一的代码。这些订单包含多个参数,每个参数都有一个唯一的代码。

  • 我得到了一个订单代码和一个参数代码,现在我必须找到正确的订单,并在该订单中找到正确的参数。

  • 对于这个参数,我需要更新“numericalValue”和“stringValue”字段。 这对我来说是困难的部分:由于上述两个条件,看起来我必须一路引入两个中间变量,每个中间变量都有一个条件 where< /code> 子句,第二个子句建立在第一个子句的基础上。

  • 然后我必须将整个 XML 文件写回到磁盘,现在包含我的两个小更改。 使用上面描述的中间变量,将整个事情再次组合在一起,这对我来说看起来像是一条特别棘手的道路。

所以这是我的 XML 文件:

<xyz version="1.2.3" 
     xmlns="http://www.mycompany.com/xyz"
     xmlns:abc="http://www.mycompany.com/xyz/abc">
  <header>
    <!-- some unimportant stuff -->
  </header>
  <body>
    <abc:abc version="1.3">
      <abc:branches>
        <abc:branch>
          <!-- some unimportant stuff -->
          <abc:orders>
            <abc:order>
check this-> <abc:orderCode>FOO</abc:orderCode>
              <abc:orderParameters>
                <abc:orderParameter>
  and this->   <abc:parameterCode>BAR</abc:parameterCode>
                  <abc:billedValue>
                    <abc:value>
       then change -> <abc:numericalValue>10</abc:numericalValue>
       these two   -> <abc:stringValue>ten</abc:stringValue>
                    </abc:value>
                  </abc:billedValue>
                </abc:orderParameter>
                <abc:orderParameter>
                  <!-- similar contents -->
                </abc:orderParameter>
                <!-- some more orderParameter instances -->
              </abc:orderParameters>
            </abc:order>
            <abc:order>
              <!-- similar contents as above -->
            </abc:order>
            <!-- some more order instances here -->
          </abc:orders>
        </abc:branch>
      </abc:branches>
    </abc:abc>
  </body>
</xyz>

正如我所说,我怀疑解决方案非常简单,但是我现在似乎无法弄清楚(尤其是我必须把整个事情写回来的部分)。

I suspect the solution to my problem to be very easy with LINQ to XML, but I'm rather new to it and just seem to run into walls. (Also, I couldn't find a decent tutorial which covers similar cases to mine, so if you can point me to anything there, it's highly appreciated!)

What I need to do is this:

  • I've got an XML file that I have to read from, which you can find below. There's usually just one branch in the file, which contains multiple orders. Every order has a unique code. Those orders contain multiple parameters, each of which also has a unique code.

  • I am given an order code and a parameter code, and I now have to find the right order, and within that order the right parameter.

  • For this one parameter, I need to update the "numericalValue" and "stringValue" fields.
    This is the hard part for me: because of the two conditions stated above, it looks like I have to have to introduce two intermediate variables along the way, each having one condition in its where clause, and the second one building on the first.

  • I then have to write the whole XML file back to disk, now containing my two little changes. With my intermediate variables described above, putting the whole thing together again like this looks like a particular thorny path for me.

So here's my XML file:

<xyz version="1.2.3" 
     xmlns="http://www.mycompany.com/xyz"
     xmlns:abc="http://www.mycompany.com/xyz/abc">
  <header>
    <!-- some unimportant stuff -->
  </header>
  <body>
    <abc:abc version="1.3">
      <abc:branches>
        <abc:branch>
          <!-- some unimportant stuff -->
          <abc:orders>
            <abc:order>
check this-> <abc:orderCode>FOO</abc:orderCode>
              <abc:orderParameters>
                <abc:orderParameter>
  and this->   <abc:parameterCode>BAR</abc:parameterCode>
                  <abc:billedValue>
                    <abc:value>
       then change -> <abc:numericalValue>10</abc:numericalValue>
       these two   -> <abc:stringValue>ten</abc:stringValue>
                    </abc:value>
                  </abc:billedValue>
                </abc:orderParameter>
                <abc:orderParameter>
                  <!-- similar contents -->
                </abc:orderParameter>
                <!-- some more orderParameter instances -->
              </abc:orderParameters>
            </abc:order>
            <abc:order>
              <!-- similar contents as above -->
            </abc:order>
            <!-- some more order instances here -->
          </abc:orders>
        </abc:branch>
      </abc:branches>
    </abc:abc>
  </body>
</xyz>

As I said, I suspect the solution is quite easy, but I can't seem to figure it out right now (especially the part where I have to write the whole thing back).

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

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

发布评论

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

评论(2

美人迟暮 2024-12-22 13:43:17

假设确保您要查找的具有该订单代码和参数代码的元素存在,以下代码片段应该会给您一个想法:

    string orderCode = "FOO";
    string paramCode = "BAR";

    XDocument doc = XDocument.Load("file.xml");

    XNamespace abc = "http://www.mycompany.com/xyz/abc";

    XElement value = 
        doc
        .Descendants(abc + "order")
        .First(o => o.Element(abc + "orderCode").Value == orderCode)
        .Descendants(abc + "orderParameter")
        .First(p => p.Element(abc + "parameterCode").Value == paramCode)
        .Element(abc + "billedValue")
        .Element(abc + "value");

    value.SetElementValue(abc + "numericalValue", 20);
    value.SetElementValue(abc + "stringValue", "twenty");

    doc.Save(Console.Out); // do doc.Save("file.xml") to overwrite

Assuming it is ensured the elements with that order code respectively parameter code you are looking for exist the following snippet should give you an idea:

    string orderCode = "FOO";
    string paramCode = "BAR";

    XDocument doc = XDocument.Load("file.xml");

    XNamespace abc = "http://www.mycompany.com/xyz/abc";

    XElement value = 
        doc
        .Descendants(abc + "order")
        .First(o => o.Element(abc + "orderCode").Value == orderCode)
        .Descendants(abc + "orderParameter")
        .First(p => p.Element(abc + "parameterCode").Value == paramCode)
        .Element(abc + "billedValue")
        .Element(abc + "value");

    value.SetElementValue(abc + "numericalValue", 20);
    value.SetElementValue(abc + "stringValue", "twenty");

    doc.Save(Console.Out); // do doc.Save("file.xml") to overwrite
最丧也最甜 2024-12-22 13:43:17

Jan - 你应该查看 这篇文章。它涵盖了查询和保存——这两个项目是您对 LINQ-to-XML 感兴趣的。

Jan - you should check out this post. It covers querying and saving - both items you are interesting in for LINQ-to-XML.

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