C # XML additions and deletions check and change

Source: Internet
Author: User

C # XML XmlDocument

To add a namespace:

Using System.Xml;

To define a common object:

XmlDocument xmldoc; XmlNode XmlNode; XmlElement Xmlelem;

1, create an XML file to the server directory with the same name:

Method One:

xmldoc = new XmlDocument ();//Add XML to the declaration paragraph, <?xml version= "1.0" encoding= "gb2312"? >xmldeclaration xmldecl;xmldecl = XmlDoc. Createxmldeclaration ("1.0", "gb2312", null); xmldoc. AppendChild (XMLDECL);//Add a root element Xmlelem = xmldoc. CreateElement ("", "Employees", ""); xmldoc. AppendChild (Xmlelem);//Add another element for (int i=1;i<3;i++) {XmlNode root=xmldoc. selectSingleNode ("Employees");//Find <Employees> XmlElement xe1=xmldoc. CreateElement ("node");//Create a <Node> node xe1. SetAttribute ("Genre", "doucube");//Set the node genre property xe1. SetAttribute ("ISBN", "2-3631-4");//Set the Node ISBN property XmlElement Xesub1=xmldoc.     CreateElement ("title"); Xesub1. Innertext= "CS from beginner to proficient";//Set Text node Xe1. AppendChild (XESUB1);//Add to the <Node> node XmlElement xesub2=xmldoc.     CreateElement ("author"); Xesub2.     Innertext= "Waiting for the Czech Republic"; Xe1.     AppendChild (XESUB2); XmlElement Xesub3=xmldoc.     CreateElement ("Price"); Xesub3.     innertext= "58.3"; Xe1.     AppendChild (XESUB3); Root. AppendChild (XE1);//Add to <Employees> node}//Save the created XML document Xmldoc.save (Server.MapPath ("Data.xml"));  

//////////////////////////////////////////////////////////////////////////////////////
Result: A file named Data.xml was generated under the same name, with the following

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< Node genre= "Doucube" isbn= "2-3631-4" ><title>cs from Getting started to mastering </title><author> waiting </author>< Price>58.3</price></node></employees>

Method Two:

XmlTextWriter xmlwriter;string strFileName = Server.MapPath ("Data1.xml"); xmlWriter = new XmlTextWriter (StrFilename, Encoding.default);//Create an XML document xmlwriter.formatting = Formatting.indented;xmlwriter.writestartdocument (); Xmlwriter.writestartelement ("Employees"); Xmlwriter.writestartelement ("Node"); Xmlwriter.writeattributestring (" Genre "," Doucube "); Xmlwriter.writeattributestring (" ISBN "," 2-3631-4 "); Xmlwriter.writestartelement (" title "); Xmlwriter.writestring ("CS from beginner to Proficient"); Xmlwriter.writeendelement (); Xmlwriter.writestartelement ("author"); Xmlwriter.writestring ("Waiting"); Xmlwriter.writeendelement (); Xmlwriter.writestartelement ("Price"); Xmlwriter.writestring ("58.3"); Xmlwriter.writeendelement (); Xmlwriter.writeendelement (); Xmlwriter.close ();

//////////////////////////////////////////////////////////////////////////////////////
Results:

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< /employees>

2, add a node:

XmlDocument xmldoc=new XmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNode Root=xmldoc.selectsinglenode ("Employees");//Find <Employees> XmlElement xe1=xmldoc.createelement ("Node ");//Create a <Node> node xe1. SetAttribute ("Genre", "Zhang San");//Set the node genre property xe1. SetAttribute ("ISBN", "1-1111-1");//Set the Node ISBN property XmlElement xesub1=xmldoc.createelement ("title"); Xesub1. Innertext= "C # Getting Started help";//Set Text node Xe1. AppendChild (XESUB1);//Add to <Node> node XmlElement 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 the <Employees> node Xmldoc.save (Server.MapPath ("Data.xml"));

//////////////////////////////////////////////////////////////////////////////////////
Result: A node is added to the original XML content as follows,

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< Node genre= "Doucube" isbn= "2-3631-4" ><title>cs from Getting started to mastering </title><author> waiting </author>< Price>58.3</price></node><node genre= "Zhang San" isbn= "1-1111-1" ><title>c# Getting Started help </title> <author> Master </author><price>158.3</price></Node></Employees>

3, modify the value of the node (attributes and sub-nodes):

XmlDocument xmldoc=new XmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNodeList Nodelist=xmldoc.selectsinglenode ("Employees"). childnodes;//get all child nodes of Employees node foreach (XmlNode xn in nodeList)//traverse all child nodes {     XmlElement xe= (XmlElement) xn;// Converts a child node type to a XmlElement type     if (XE). GetAttribute ("genre") = = "Zhang San")//if the genre attribute value is "Zhang San"     {         XE. SetAttribute ("Genre", "Update Zhang San");//Modify this property to "update Zhang San"         XmlNodeList nls=xe. childnodes;//continue to acquire all child nodes of the XE subnodes         foreach (XmlNode xn1 in NLS)//traversal         {             XmlElement xe2= (XmlElement) xn1;//conversion type             if (xe2. name== "Author")//If you find             {                 xe2. innertext= "Ya sheng";//Modify            }}}     Xmldoc.save (Server.MapPath ("Data.xml"));//save.

//////////////////////////////////////////////////////////////////////////////////////
Result: The original information of all the nodes have been modified, the content of the XML is as follows,

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< Node genre= "Doucube" isbn= "2-3631-4" ><title>cs from Getting started to mastering </title><author> waiting </author>< Price>58.3</price></node><node genre= "Update Zhang San" isbn= "1-1111-1" ><title>c# Getting Started help </ Title><author> </author><price>158.3</price></Node></Employees>

4, modify the node (add the node's attributes and add the node's self-node):

XmlDocument xmldoc=new XmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNodeList Nodelist=xmldoc.selectsinglenode ("Employees"). childnodes;//gets all child nodes of the Employees node foreach (XmlNode xn in nodeList) {     XmlElement xe= (XmlElement) xn;     Xe. SetAttribute ("Test", "111111");    XmlElement xesub=xmldoc.createelement ("flag");     Xesub. innertext= "1";     Xe. AppendChild (xesub); } xmldoc.save (Server.MapPath ("Data.xml"));

//////////////////////////////////////////////////////////////////////////////////////
Result: The attributes of each node are added with one, and the sub-nodes are also added, the contents are as follows,

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" test= "111111 "><title>cs from beginner to proficient </title><author> </author><price>58.3</price><flag >1</flag></node><node genre= "Doucube" isbn= "2-3631-4" test= "111111" ><title>cs from getting started to mastering </title><author> </author><price>58.3</price><flag>1</flag></ Node><node genre= "Update Zhang San" isbn= "1-1111-1" test= "111111" ><title>c# Getting Started help </title><author > Ya sheng </author><price>158.3</price><flag>1</flag></Node></Employees>

5, delete one of the properties in the node:

XmlDocument xmldoc=new XmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNodeList Xnl=xmldoc.selectsinglenode ("Employees"). ChildNodes; foreach (XmlNode xn in xnl) {     XmlElement xe= (XmlElement) xn;     Xe. RemoveAttribute ("genre");//Delete genre property     XmlNodeList Nls=xe. childnodes;//continue to acquire all child nodes of the XE subnodes     foreach (XmlNode xn1 in NLS)//traversal     {         XmlElement xe2= (XmlElement) xn1;//conversion type         if (xe2. name== "flag")//If you find         {             XE. RemoveChild (XE2);//Delete        }     

//////////////////////////////////////////////////////////////////////////////////////]
Result: Delete A property of the endpoint and a sub-node of the node, as follows,

<?xml version= "1.0" encoding= "gb2312"? ><employees><node isbn= "2-3631-4" test= "111111" ><title >cs from beginner to proficient </title><author> </author><price>58.3</price></node><node Isbn= "2-3631-4" test= "111111" ><title>cs from Getting started to mastering </title><author> waiting </author><price >58.3</price></node><node isbn= "1-1111-1" test= "111111" ><title>c# Getting Started help </title> <author> ya sheng </author><price>158.3</price></Node></Employees>

6. Delete the node:

XmlDocument xmldoc=new XmlDocument (); Xmldoc.load (Server.MapPath ("Data.xml")); XmlNode Root=xmldoc.selectsinglenode ("Employees"); XmlNodeList Xnl=xmldoc.selectsinglenode ("Employees"). ChildNodes; for (int i=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;    

//////////////////////////////////////////////////////////////////////////////////////]
Result: Delete all the nodes that match the condition, the original content:

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< Node genre= "Doucube" isbn= "2-3631-4" ><title>cs from Getting started to mastering </title><author> waiting </author>< Price>58.3</price></node><node genre= "Zhang San" isbn= "1-1111-1" ><title>c# Getting Started help </title> <author> Master </author><price>158.3</price></node><node genre= "Zhang San" isbn= "1-1111-1" ><title>c# Getting Started help </title><author> expert </author><price>158.3</price></node ></Employees>

Content after deletion:

<?xml version= "1.0" encoding= "gb2312"? ><employees><node genre= "Doucube" isbn= "2-3631-4" >< Title>cs from getting started to mastering </title><author> waiting </author><price>58.3</price></Node>< Node genre= "Doucube" isbn= "2-3631-4" ><title>cs from Getting started to mastering </title><author> waiting </author>< Price>58.3</price></node></employees>

7, read the XML as a text file

System.IO.StreamReader myFile = new System.IO.StreamReader (Server.MapPath ("Data.xml"), System.Text.Encoding.Default );//Note System.Text.Encoding.Defaultstring myString = Myfile.readtoend ();//mystring is a read-out string myfile.close ();

C # XML additions and deletions check and change

Related Article

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.