Modify an XML Element using LINQ - 2

Madhu.B.Rokkam
Posted by Madhu.B.Rokkam under C# category on | Points: 40 | Views : 763
public static void Main()
{
string serializedXml = "<Root><Students><Student>1</Student><Name>Old Name</Name><Age>33</Age></Students></Root>";

XDocument xmlDoc = XDocument.Parse(serializedXml);
xmlDoc.Descendants("Students").Elements("Name").Single().SetValue("New Name");

Console.WriteLine(xmlDoc);
Console.Read();
}


Output

<Root>
<Students>
<Student>1</Student>
<Name>New Name </Name>
<Age>33</Age>
</Students>
</Root>

Comments or Responses

Login to post response