locked
Open an Xml File and change somethin RRS feed

  • Question

  • <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <other>
        <Data key="Name">Hans</Data>
      <other>
    <configuration>

    Hello there, I have a litle problem. I d'like to change "Hans" to "Josef" with C#. The problem is that the thing i d'like to change is between the two "Data" tages. I can change the key but not the value inside the tags.

    Thank you

    Sunilplayer

    Thursday, May 24, 2018 8:39 AM

All replies

  • Hello, Sunilplayer.
    This sample code can help you:

    var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <configuration>
      <other>
        <Data key=""Name"">Hans</Data>
      </other>
    </configuration>";
    
    var xDocument = XDocument.Parse(xml);
    var xElement = xDocument.XPathSelectElement("//Data[@key='Name']");
    xElement.Value = "Josef";

    Then xDocument.ToString() will return:

    <configuration>
      <other>
        <Data key="Name">Josef</Data>
      </other>
    </configuration>

    • Proposed as answer by Dolen Zhang Friday, May 25, 2018 6:08 AM
    Thursday, May 24, 2018 10:38 AM