help with Linq to update xml

  • Thread starter Thread starter GiJeet
  • Start date Start date
G

GiJeet

Hello, I have an xml fragment where I sum certain elements. code is
below. Once I have the total amount, I need to use SetElementValue to
update a certain element but not sure how to do that. Code example
would be appreciated. Below is the xml fragment and the code to
extract the sum. Also, I'd like to change the method to use dot
notation rather then query syntax. Any help with that would also be
appreciated.

The xml fragment:
<taSopLineIvcTaxInsert_Items>
<taSopLineIvcTaxInsert>
<SOPTYPE>3</SOPTYPE>
<TAXTYPE>0</TAXTYPE>
<SOPNUMBE>INV2042V</SOPNUMBE>
<CUSTNMBR>CELLULAR0001</CUSTNMBR>
<SALESAMT>69.75000</SALESAMT>
<FRTTXAMT>0.00000</FRTTXAMT>
<MSCTXAMT>0.00000</MSCTXAMT>
<FREIGHT>0.00000</FREIGHT>
<MISCAMNT>0.00000</MISCAMNT>
<TAXSCHID>USASTE-PS6N0</TAXSCHID>
<LNITMSEQ>0</LNITMSEQ>
<STAXAMNT>0</STAXAMNT>
</taSopLineIvcTaxInsert>
<taSopLineIvcTaxInsert>
<SOPTYPE>3</SOPTYPE>
<TAXTYPE>0</TAXTYPE>
<SOPNUMBE>INV2042V</SOPNUMBE>
<CUSTNMBR>CELLULAR0001</CUSTNMBR>
<SALESAMT>69.75000</SALESAMT>
<FRTTXAMT>0.00000</FRTTXAMT>
<MSCTXAMT>0.00000</MSCTXAMT>
<FREIGHT>0.00000</FREIGHT>
<MISCAMNT>0.00000</MISCAMNT>
<TAXSCHID>USASTE-PS6N0</TAXSCHID>
<LNITMSEQ>16384</LNITMSEQ>
<STAXAMNT>1.80000</STAXAMNT>
</taSopLineIvcTaxInsert>
<taSopLineIvcTaxInsert>
<SOPTYPE>3</SOPTYPE>
<TAXTYPE>0</TAXTYPE>
<SOPNUMBE>INV2042V</SOPNUMBE>
<CUSTNMBR>CELLULAR0001</CUSTNMBR>
<SALESAMT>69.75000</SALESAMT>
<FRTTXAMT>0.00000</FRTTXAMT>
<MSCTXAMT>0.00000</MSCTXAMT>
<FREIGHT>0.00000</FREIGHT>
<MISCAMNT>0.00000</MISCAMNT>
<TAXSCHID>USASTE-PS6N0</TAXSCHID>
<LNITMSEQ>32768</LNITMSEQ>
<STAXAMNT>2.40000</STAXAMNT>
</taSopLineIvcTaxInsert>
<taSopLineIvcTaxInsert>
<SOPTYPE>3</SOPTYPE>
<TAXTYPE>0</TAXTYPE>
<SOPNUMBE>INV2042V</SOPNUMBE>
<CUSTNMBR>CELLULAR0001</CUSTNMBR>
<SALESAMT>69.75000</SALESAMT>
<FRTTXAMT>0.00000</FRTTXAMT>
<MSCTXAMT>0.00000</MSCTXAMT>
<FREIGHT>0.00000</FREIGHT>
<MISCAMNT>0.00000</MISCAMNT>
<TAXSCHID>USCITY-PS1N0</TAXSCHID>
<LNITMSEQ>49152</LNITMSEQ>
<STAXAMNT>0.30000</STAXAMNT>
</taSopLineIvcTaxInsert>
<taSopLineIvcTaxInsert>
<SOPTYPE>3</SOPTYPE>
<TAXTYPE>0</TAXTYPE>
<SOPNUMBE>INV2042V</SOPNUMBE>
<CUSTNMBR>CELLULAR0001</CUSTNMBR>
<SALESAMT>69.75000</SALESAMT>
<FRTTXAMT>0.00000</FRTTXAMT>
<MSCTXAMT>0.00000</MSCTXAMT>
<FREIGHT>0.00000</FREIGHT>
<MISCAMNT>0.00000</MISCAMNT>
<TAXSCHID>USCITY-PS1N0</TAXSCHID>
<LNITMSEQ>65536</LNITMSEQ>
<STAXAMNT>0.40000</STAXAMNT>
</taSopLineIvcTaxInsert>
</taSopLineIvcTaxInsert_Items>

method to get the sum:
private double GetTotalTax(XElement frag) {
return (from x in frag.Elements("taSopLineIvcTaxInsert")
where (int)x.Element("LNITMSEQ") != 0
select (double)x.Element("STAXAMNT")).Sum();
}

I need to update the STAXAMNT element where LNITMSEQ element = 0.
it would be great if we can do both in one method.

Thanks in Advance.
 
I've got some ideas that may be useful. First you should study LIQ to XML
and secondly determine which types of container objects will work well for
you. Secondly, "dot notation" is used in non-traditional ways and it is
apparently called Fluent. Finally, you need to read about XPath to get at
the data in the elements.
 
Back
Top