Home > Back-end >  How to Append string to an Existing XML file
How to Append string to an Existing XML file

Time:08-14

The Below is my XMl File

 <?xml version="1.0" encoding="utf-8"?>

    <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">      
      <string>PipeFabricationAddin</string>
      <string>IntegratorAddin</string>
    </ArrayOfString>

I'm new to a XML world using C#.. is there any way to append <string> Something </string> inside the <ArrayOfString></ArrayOfString>?

What I want is this.

 <?xml version="1.0" encoding="utf-8"?>

    <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">      
      <string>PipeFabricationAddin</string>
      <string>C:\AVEVA\Plant\PDMS12.1.SP4\PDMSAddin\PEDASConverter_PDMS</string> !!! i want this line to be added!!!
      <string>IntegratorAddin</string>
    </ArrayOfString>

thanks for reading my question.

CodePudding user response:

You can do this pretty simply with XDocument

var xDoc = XDocument.Load(filePath);   // alternatively XDocuemnt.Parse

xDoc.Root.Elements()
    .First(e => e.Value == "PipeFabricationAddin")
    .AddAfterSelf(new XElement("string", "C:\AVEVA\Plant\PDMS12.1.SP4\PDMSAddin\PEDASConverter_PDMS"));

xDoc.Save(filePath);
  • Related