"C #" How to create an XML file and an XML file to add and change

Source: Internet
Author: User

Increase:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text.RegularExpressions;usingsystem.web;usingSystem.Xml;namespaceappservice{ Public classXMLReader { Public Static voidXmlmake (stringindesstring) {            Try            {                strings =indesstring; string[] resultstring = S.split ('|'); foreach(stringIinchresultstring) {} XmlDocument Doc=NewXmlDocument (); XmlDeclaration Dec= Doc. Createxmldeclaration ("1.0","UTF-8",NULL); Doc.                AppendChild (DEC); //Create a root node (first level)XmlElement root = doc.createelement_x ("Jokes"); Doc.                AppendChild (root); //Create nodes (level two)XmlNode node = doc.createelement_x ("Tag"); //Create nodes (level three)XmlElement element1 = doc.createelement_x ("Shenchang"); //element1.                SetAttribute ("Name", "Sam"); //element1. SetAttribute ("ID", "665");Element1. InnerText = resultstring[0]; Node.                AppendChild (ELEMENT1); XmlElement Element2= Doc.createelement_x ("Tizhong"); //Element2.                SetAttribute ("Name", "Round"); //Element2. SetAttribute ("ID", "678");Element2. InnerText = resultstring[1]; Node.                AppendChild (Element2); XmlElement Element3= Doc.createelement_x ("BMI"); //Element2.                SetAttribute ("Name", "Round"); //Element2. SetAttribute ("ID", "678");Element3. InnerText = resultstring[2]; Node.                AppendChild (ELEMENT3); Root.                AppendChild (node); stringp = AppDomain.CurrentDomain.BaseDirectory +"Version1"; Doc. Save (P+"\\ABC.xml"); Console.Write (Doc.            OuterXml); }            Catch            { }        }    }}

Change:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Xml;namespaceappservice{ Public classXmlwrite { Public Static voidxmlupdate (String indesstring) {strings =indesstring; string[] resultstring = S.split ('|'); Try{XmlDocument xmldoc=NewXmlDocument (); stringp = AppDomain.CurrentDomain.BaseDirectory +"Version1"; Xmldoc.load (P+"\\ABC.xml"); XmlNode Xns= Xmldoc.selectsinglenode ("Jokes"); XmlNodeList XNL=Xns.                ChildNodes; foreach(XmlNode xninchxnl) {XmlElement XE=(XmlElement) xn; XmlNodeList XNL2=XE.                    ChildNodes; foreach(XmlNode xn2inchxnl2) {XmlElement Xe2=(XmlElement) xn2; if(Xe2. Name = ="Shenchang") {Xe2. InnerText= resultstring[0]; }                        if(Xe2. Name = ="Tizhong") {Xe2. InnerText= resultstring[1]; }                        if(Xe2. Name = ="BMI") {Xe2. InnerText= resultstring[2]; }}} xmldoc.save (P+"\\ABC.xml"); }            Catch            {                           }        }    }}

"C #" How to create an XML file and an XML file to add and change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.