Search code examples
c#xmllinq-to-xmlxmldocument

Need to add new items under the existing XML file using C#


I need to add new items in the existing file using C#, Could any please provide best logic to do it in using C#. Below is my XML File (Input)=

<?xml version="1.0" encoding="UTF-8"?>
<FileSetting xmlns="http://ltsc.ieee.org/xsd/LOM" xmlns:xs="http://www.M1.org/2001/XMLSchema">
   <Files>
      <File Section="Section 1" Method="Complete" Location="Total 1">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <!--  <File Section="Section 2" Method="Complete" Location="Total 2">
    <Columns>
      <Profile Method= "DataCollection" Item="All"/>
     </Columns>
  </File> -->
      <File Section="Section 3" Method="Complete" Location="Main">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
   </Files>
</FileSetting>

Here in my existing XML file I want to add new File item under the Files, so I am expecting output as=

<?xml version="1.0" encoding="UTF-8"?>
<FileSetting xmlns="http://ltsc.ieee.org/xsd/LOM" xmlns:xs="http://www.M1.org/2001/XMLSchema">
   <Files>
      <File Section="Section 1" Method="Complete" Location="Total 1">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <!--  <File Section="Section 2" Method="Complete" Location="Total 2">
    <Columns>
      <Profile Method= "DataCollection" Item="All"/>
     </Columns>
  </File> -->
      <File Section="Section 3" Method="Complete" Location="Main">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <File Section="Section 4" Method="NotComplete" Location="Test5">
         <Columns>
            <Profile Method="DataCollecter" Item="Partial" />
         </Columns>
      </File>
   </Files>
</FileSetting>

Could anyone please provide the best logic to do it in C#?

Thanks in Advance


Solution

  • Use xml linq :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                XNamespace ns = doc.Root.GetDefaultNamespace();
    
                XElement files = doc.Descendants(ns + "Files").FirstOrDefault();
    
                XElement newFile = new XElement(ns + "File", new object[] {
                    new XAttribute("Section", "Section 4"),
                    new XAttribute("Method", "NotComplete"),
                    new XAttribute("Location", "Test5"),
                    new XElement(ns + "Columns", new object[] {
                        new XElement("Profile", new object[] {
                            new XAttribute("Method", "DataCollecter"),
                            new XAttribute("Item", "Partial")
                        })
                    })
                });
    
                files.Add(newFile);
            }
        }
    }