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> <Tittle Type="BooksName">C#7.0 Core Technology Guide</Title> <Price Type="RMB">259.00</Price> </Book> </BookStory>
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:
<?xml version="1.0" encoding="utf-8"?> <XML> <Conn Name="Conn1" Type="SQL Server Database"> <Source> <SServer>123.123.123</SServer> <SIdentity /> <SDataBase /> <SUserID /> <SPwd /> </Source> <Target> <TServer>00000000</TServer> <TIdentity /> <TDataBase /> <TUserID /> <TPwd /> </Target> </Conn> </XML>
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.