First, the general introduction
//initializing an XML objectXmlDocument XML =NewXmlDocument (); //loading an XML fileXml. Load ("file path"); //reads the specified nodeXmlNode XmlNode = XML. selectSingleNode ("/Node Name"); //determine if there are child nodes under the nodeXmlnode.haschildnodes; //Read all child nodes under a nodeXmlNodeList XmlNodeList =Xmlnode.childnodes; //Read List of multiple nodes with same name siblingXmlNodeList XmlNodeList = XML. SelectNodes ("/root/node Name"); //read the properties of a node stringattribute = xmlnode.attributes["Property name"]. Value; //read the text of a node stringValue =Xmlnode.innertext; //to create a new nodeXmlNode XmlNode = XML. CreateElement ("Node name"); //Save the XML fileXml. Save ("file path");
Two, add a node
XmlDocument xmldoc=NewXmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNode Root=xmldoc.selectsinglenode ("Employees");//Find <Employees>XmlElement Xe1=xmldoc.createelement ("Node");//Create a <Node> nodeXe1. SetAttribute ("genre","Zhang San");//set the node genre propertyXe1. SetAttribute ("ISBN","1-1111-1");//Set the Node ISBN propertyXmlElement Xesub1=xmldoc.createelement ("title"); Xesub1. InnerText="C # Getting Started help";//Set the text nodeXe1. AppendChild (XESUB1);//Add to <Node> nodeXmlElement Xesub2=xmldoc.createelement ("author"); Xesub2. InnerText="Master"; Xe1. AppendChild (XESUB2); XmlElement xesub3=xmldoc.createelement (" Price"); Xesub3. InnerText="158.3"; Xe1. AppendChild (XESUB3); Root. AppendChild (XE1);//Add to <Employees> nodeXmldoc.save (Server.MapPath ("Data.xml") );
Third, edit the node
XmlDocument xmldoc =NewXmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNodeList NodeList= Xmldoc.selectsinglenode ("Employees"). ChildNodes;//all child nodes of the employees node are obtained foreach(XmlNode xninchNodeList)//Traverse all child nodes{XmlElement XE= (XmlElement) xn;//To Convert a child node type to a XmlElement type if(XE. GetAttribute ("genre") =="Zhang San")//if the genre property value is "Zhang San"{XE. SetAttribute ("genre","Update Zhang San");//then modify the property to "update Zhang San"XmlNodeList NLS = Xe. ChildNodes;//continue to acquire all child nodes of the XE child node foreach(XmlNode xn1inchNls//Traverse{XmlElement Xe2= (XmlElement) xn1;//Conversion Type if(Xe2. Name = ="author")//If you find{Xe2. InnerText="Asian Victory";//then modify}}}} Xmldoc.save (Server.MapPath ("Data.xml"));//saved.
Iv. deleting a property in a node
XmlDocument xmldoc =NewXmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNodeList XNL= Xmldoc.selectsinglenode ("Employees"). ChildNodes; foreach(XmlNode xninchxnl) {XmlElement XE=(XmlElement) xn; Xe. RemoveAttribute ("genre");//Delete Genre PropertyXmlNodeList NLS = Xe. ChildNodes;//continue to acquire all child nodes of the XE child node foreach(XmlNode xn1inchNls//Traverse{XmlElement Xe2= (XmlElement) xn1;//Conversion Type if(Xe2. Name = ="Flag")//If you find{XE. RemoveChild (XE2);//the Delete}}} xmldoc.save (Server.MapPath ("Data.xml"));
Vi. Deleting nodes
XmlDocument xmldoc =NewXmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNode Root= Xmldoc.selectsinglenode ("Employees"); XmlNodeList XNL= Xmldoc.selectsinglenode ("Employees"). ChildNodes; for(inti =0; I < XNL. Count; i++) {XmlElement XE=(XmlElement) xnl. Item (i); if(XE. GetAttribute ("genre") =="Zhang San") {root. RemoveChild (XE); if(I < XNL. Count) i = i-1; }} xmldoc.save (Server.MapPath ("Data.xml"));
Read XML by text
System.IO.StreamReader myFile =new System.IO.StreamReader (Server.MapPath (" Data.xml"), System.Text.Encoding.Default); // Note System.Text.Encoding.Default string myString = Myfile.readtoend (); // MyString is a read-out string Myfile.close ();
. NET XML Operations