I have a xml document that has a record set like this.
<document>
<row>
<Pub_Code>OHB-A0011</Pub_Code>
<Sec>16</Sec>
<Pags>20</Pags>
<Copies>1,000</Copies>
<Binding>Saddle Stitch</Binding>
<Tab>No tabs</Tab>
<Qty>0</Qty>
<Cover>Self Cover</Cover>
<Tpgs>0</Tpgs>
</row>
</document>
I have a linq query wrtten this way:
string xml_path = @"D:\Server-Apps\BooksData.xml";
XElement root = XElement.Load(xml_path);
var selected = from myBooks in root.Elements("row") where myBooks.Element("Pub_Code").Value == "OHB-A0011" select myBooks;
foreach (var d in selected)
{
Console.WriteLine("Pub_Code: {0}", d.Element("Pub_Code").Value);
Console.WriteLine("Cover: {0}", d.Element("Cover").Value);
d.SetElementValue("Tpgs", "test");
}
I can read the value find but when I uses d.SetElementValue(“Tpgs”, “test”); nothing gets update.
the tag is already in the xml file .
If you want to save the updated XML back to the file, you need to do:
I added the line t the end of your program and it seems to work correctly.