c# - Subtracting from an XML file -
i have xml file contains stock of tyres nodes this
<stock> <tyre> <manufacturer>yokohama</manufacturer> <quantity>500</quantity> </tyre> </stock>
on form have order form can select manufacturer of tyre in textbox or combobox drop down, , enter amount in textbox , subtract , update quantity node.
would have each loop , through each tyre manufacturer node name entered textbox?
foreach (xmlnode xnode in xdoc.selectnodes("stock/tyre")) { stock st = new stock(); st.manufacturer = xnode.selectsinglenode("manufacturer ").innertext; //??? }
where comment ? stuck how subtract value in textbox stock quantity in xml file.
should doing way or wrong?
this can done more , elegant using linq xml:
var filename = @"c:\test.xml"; var manufacturer = "yokohama"; var amount = 100; var doc = xdocument.load(filename); var node = doc.xpathselectelements("stock/tyre/manufacturer") .firstordefault(x => x.value == manufacturer); if (node != null) { var valuenode = node.parent.xpathselectelement("quantity"); if (valuenode != null) valuenode.setvalue(convert.toint32(valuenode.value) - amount); } doc.save(filename);
Comments
Post a Comment