SoFunction
Updated on 2025-03-07

C# write to XML documents

1. Case 1

        //XML file writing method        //Write the XML file format and store it to the specified FilePath (path)        internal void WriterXML(string FilePath)
        {

            try
            {
                XmlDocument doc = new XmlDocument();//Declare an XmlDocument as a container for XML documents                //XmlDeclaration xmlDec = ("1.0", "UTF-8", "yes");
                XmlElement BookStory = ("BookStory");//XMLElement represents the beginning of a tag or element                XmlAttribute Address = ("Address");//XmlAttribute represents the attribute of a tag or element                 = "Jimei New Pavilion, Xiamen City, Fujian Province";//InnerText represents the content of a certain element                ("Type", "On_Line");
                ("CreatTime", ());
                XmlElement Book = ("Book");
                XmlElement Tittle = ("Tittle");
                ("Type", "BooksName");
                 = "C#7.0 Core Technology Guide";                XmlElement Price = ("Price");
                ("Type", "RMB");
                 = "259.00";
                //(xmlDec);
                //Add node                (BookStory);
                //BookStory adds child nodes                (Book);
                (Address);
                //Book Add child nodes                (Tittle);
                (Price);

                XmlWriterSettings settings = new XmlWriterSettings();//Specify the functions implemented by XMLWriter class                 = true;
                 = Encoding.UTF8;//Define the encoding format                XmlWriter writer = (FilePath, settings);//Create method uses the XmlWriterSettings class to specify what functions to implement in the created XmlWriter object.                //Write to file                (writer);
                ();//Clear the cache area                ();
                ("This Ok!");
            }
            catch (Exception ex)
            {
                ();
            }
        }

Write content:

<?xml version="1.0" encoding="utf-8"?>
<BookStory Type="On_Line" CreatTime="06:04:50 pm" Address="Jimei New Pavilion, Xiamen City, Fujian Province">
  <Book>
    &lt;Tittle Type="BooksName"&gt;C#7.0 Core Technology Guide</Title>    &lt;Price Type="RMB"&gt;259.00&lt;/Price&gt;
  &lt;/Book&gt;
&lt;/BookStory&gt;

2. Case 2

        //XML file is written to database synchronization basic data and XML design format        internal void WriterInfo(string FilePath)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement XML = ("XML");
            XmlElement Conn = ("Conn");
            ("Name", "Conn1");
            ("Type", "SQL Server Database");
            XmlElement Source = ("Source");
            XmlElement SServer = ("SServer");
            XmlElement SIdentity = ("SIdentity");
            XmlElement SDataBase = ("SDataBase");
            XmlElement SUserID = ("SUserID");
            XmlElement SPwd = ("SPwd");
            XmlElement Target = ("Target");
            XmlElement TServer = ("TServer");
            XmlElement TIdentity = ("TIdentity");
            XmlElement TDataBase = ("TDataBase");
            XmlElement TUserID = ("TUserID");
            XmlElement TPwd = ("TPwd");
            (XML);
            (Conn);
            (Source);
            (SServer);
             = "123.123.123";
            (SIdentity);
            (SDataBase);
            (SUserID);
            (SPwd);

            (Target);
            (TServer);
             = "00000000";
            (TIdentity);
            (TDataBase);
            (TUserID);
            (TPwd);
        
            XmlWriterSettings settings = new XmlWriterSettings();
             = true;
             = Encoding.UTF8;
            XmlWriter writer = (FilePath, settings);
            (writer);
            ();
            ();
            ("It's Ok!");
        }

Write content:

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;XML&gt;
  &lt;Conn Name="Conn1" Type="SQL Server Database"&gt;
    &lt;Source&gt;
      &lt;SServer&gt;123.123.123&lt;/SServer&gt;
      &lt;SIdentity /&gt;
      &lt;SDataBase /&gt;
      &lt;SUserID /&gt;
      &lt;SPwd /&gt;
    &lt;/Source&gt;
    &lt;Target&gt;
      &lt;TServer&gt;00000000&lt;/TServer&gt;
      &lt;TIdentity /&gt;
      &lt;TDataBase /&gt;
      &lt;TUserID /&gt;
      &lt;TPwd /&gt;
    &lt;/Target&gt;
  &lt;/Conn&gt;
&lt;/XML&gt;

This is all about this article about writing XML documents in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.