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