usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Threading.Tasks;usingSystem.Xml;usingSystem.IO;namespaceconsoleapplication1{/// <summary> ///XML Operation Classes/// </summary> Public classProgram {/// <summary> ///Entrance Method/// </summary> /// <param name= "args" >parameter list</param> Public Static voidMain (string[] args) { stringFilepath="Cars.xml"; Createxmlfile (FilePath); Readxmlfile (FilePath); Modifyxmlfile (FilePath); Deletexmlnode (FilePath); } /// <summary> ///Create an XML file/// </summary> /// <param name= "FilePath" >file path</param> Public Static voidCreatexmlfile (stringFilePath) { //01. Declaration XmlDocumentXmlDocument doc =NewXmlDocument (); //02. Adding head declarations to themXmlDeclaration declaration = doc. Createxmldeclaration ("1.0","Utf-8",""); Doc. AppendChild (declaration); //03. Adding a child node to itXmlElement root = Doc. CreateElement ("Cars"); Root. SetAttribute ("ID","10001");//adding attributes to a node//add Mercedes Car infoXmlElement Benz = Doc. CreateElement ("Benz"); XmlElement Benzname= Doc. CreateElement ("Name"); Benzname.innertext="Mercedes";//InnerTextBenz. AppendChild (Benzname); XmlElement Benzmaxspeed= Doc. CreateElement ("maxspeed"); Benzmaxspeed.innertext=" -"; Benz. AppendChild (Benzmaxspeed); Root. AppendChild (Benz); //add BMW car infoXmlElement BMW = Doc. CreateElement ("BMW"); bmw. INNERXML="<Name> BMW </Name><MaxSpeed>299</MaxSpeed>";//InnerXmlRoot. AppendChild (BMW); //04. Save the fileDoc. AppendChild (root); Doc. Save (FilePath); } /// <summary> ///reading an XML file/// </summary> /// <param name= "FilePath" >file path</param> Public Static voidReadxmlfile (stringFilePath) { //First Method: Use XmlDocument.Load () if(File.exists (FilePath)) {//01. Loading an XML documentXmlDocument doc =NewXmlDocument (); Doc. Load (FilePath); //02. Read the root node and child nodesXmlNode root = Doc. selectSingleNode ("Cars"); if(Root. HasChildNodes) {XmlNodeList xnd=Root. ChildNodes; foreach(XmlNode xninchxnd) {XmlElement XE=(XmlElement) xn; Console.WriteLine (xn. INNERXML); } } } //second method: Use Xmldocument.loadxml () if(File.exists (FilePath)) {using(StreamReader sr =NewStreamReader (FilePath)) { stringXmlstr =Sr. ReadToEnd (); XmlDocument Doc=NewXmlDocument (); Doc. LOADXML (XMLSTR); //The following code is the same as above } } //the difference between Load () and Loadxml () is://The former is loaded into the XML document//The latter loads the XML string } /// <summary> ///Modifying XML Document Information/// </summary> /// <param name= "FilePath" >Document Path</param> Public Static voidModifyxmlfile (stringFilePath) { if(File.exists (FilePath)) {//01. Loading an XML documentXmlDocument doc =NewXmlDocument (); Doc. Load (FilePath); XmlElement Root= (XmlElement) doc. selectSingleNode ("Cars"); //02. Modifying Attribute InformationRoot. SetAttribute ("ID","998"); //03. Modify the node informationXmlNode bwnmaxspeed = (XmlElement) doc. selectSingleNode ("Cars/bmw/maxspeed"); Bwnmaxspeed.innertext="319"; //04. Save the documentDoc. Save ("Mcar.xml"); } } /// <summary> ///Delete node or attribute information/// </summary> /// <param name= "FilePath" >XML Document Path</param> Public Static voidDeletexmlnode (stringFilePath) { if(File.exists (FilePath)) {//01. Loading DocumentsXmlDocument doc =NewXmlDocument (); Doc. Load (FilePath); XmlElement Root= (XmlElement) doc. selectSingleNode ("Cars"); //02. Delete Attribute Information if(Root. Hasattribute ("ID") {root. RemoveAttribute ("ID"); } //03. Delete Node InformationXmlNodeList Xnd =Root. ChildNodes; foreach(XmlNode xninchxnd) {XmlNode Subxn=xn. FirstChild; if(Subxn.innertext = ="BMW") {root. RemoveChild (xn); } } //04. Save ChangesDoc. Save ("Dcar.xml"); } } }}
C # manipulating XML