[Solved] Update xml node data, how [duplicate]


Try my solution,

string xml = @"<categories>
                <category>
                <id>1</id>
                <name>Computer</name>
                <description>Information tech.</description>
                <active>False</active>
                </category>
                <category>
                <id>2</id>
                <name>Cate1</name>
                <description>MMukh</description>
                <active>True</active>
                </category>
            </categories>";

XDocument xDoc = XDocument.Parse(xml);
int id = 1;

var items = xDoc.XPathSelectElement("//category[id=" + id + "]")
            .Elements()
            .ToDictionary(e => e.Name.LocalName, e => (string)e);

if (items != null)
{
    // display these fields to the text box
    Console.WriteLine(items["name"]);
    Console.WriteLine(items["description"]);
    Console.WriteLine(items["active"]);
}

you can use the above code to display data in textbox. and below code to update the existing xml with updated data.

// updated value for xml element - description
string description = "Computer 123";

var items1 = from item in xDoc.Descendants("category")
                where item.Element("id").Value == id.ToString()
                select item;

foreach (XElement itemElement in items1)
{
    itemElement.SetElementValue("name", description);
}
xDoc.Save("data.xml");

you can get the updated xml data in xDoc.

0

solved Update xml node data, how [duplicate]