2

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).

Jan
  • 683
  • 10
  • 18

2 Answers2

2

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
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

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

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • Thanks for your answer! My main problem is the *conditional* part though - I need to get a handle on exactly these two instances that I want to change, and then write them back into the otherwise unchanged XML document that I received. – Jan Nov 11 '11 at 16:50