XML read/write demo program (2)

Source: Internet
Author: User

 

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Diagnostics;
Using System. Xml;
Using System. IO;

Namespace read/write xml
{
Public partial class Form1: Form
{
String xmlPath = Application. StartupPath + "\ demo. xml ";

XmlDocument xmlDoc = new XmlDocument ();
Public Form1 ()
{
InitializeComponent ();
}

 

* ****** Void button2_Click (object sender, EventArgs e)
{

XmlDoc. Load (xmlPath );
XmlNode root = xmlDoc. SelectSingleNode ("bookstore"); // query <bookstore>
XmlElement xe1 = xmlDoc. CreateElement ("book"); // create a <book> node
Xe1.SetAttribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
Xe1.SetAttribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.

XmlElement xesub1 = xmlDoc. CreateElement ("title ");
Xesub1.InnerText = "CS from entry to entry"; // set a text node
Xe1.AppendChild (xesub1); // Add it to the <book> node
XmlElement xesub2 = xmlDoc. CreateElement ("author ");
Xesub2.InnerText = "Hou Jie ";
Xe1.AppendChild (xesub2 );
XmlElement xesub3 = xmlDoc. CreateElement ("price ");
Xesub3.InnerText = "58.3 ";
Xe1.AppendChild (xesub3 );

Root. AppendChild (xe1); // Add it to the <bookstore> node
XmlDoc. Save (xmlPath );
WebBrowser1.Navigate (xmlPath );

}

* ****** Void button3_Click (object sender, EventArgs e)
{
File. Copy (Application. StartupPath + "\ demo1.xml", xmlPath, true );
WebBrowser1.Navigate (xmlPath );
}

* ****** Void button4_Click (object sender, EventArgs e)
{
// Modify node attributes
XmlDoc. Load (xmlPath );
XmlNodeList nodeList = xmlDoc. SelectSingleNode ("bookstore"). ChildNodes; // obtain all the subnodes of the bookstore Node
Foreach (XmlNode xn in nodeList) // traverses all subnodes
{
Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
If (Xe. getattribute ("genre") = "") // If the genre attribute value is ""
{
Xe. setattribute ("genre", "Update lizan red"); // you can change this attribute to "Update lizan red"

Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
Foreach (xmlnode xn1 in NLS) // traverse
{
XmlElement xe2 = (XmlElement) xn1; // Conversion Type
If (xe2.Name = "author") // if you find
{
Xe2.InnerText = "Yasheng"; // modify
Break; // find and exit.
}
}
Break;
}
}

XmlDoc. Save (xmlPath); // Save.
Webbrowser1.navigate (xmlpath );

}

* ****** Void button5_click (Object sender, eventargs E)
{
Xmldoc. Load (xmlpath );
Xmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore"). childnodes;
 
Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;
If (Xe. getattribute ("genre") = "Fantasy ")
{
Xe. removeattribute ("genre"); // Delete genre attributes
}
Else if (Xe. getattribute ("genre") = "Update lizanhong ")
{
Xe. removeall (); // delete all content of the node
}
}
XmlDoc. Save (xmlPath );
WebBrowser1.Navigate (xmlPath );

}

* ****** Void button6_Click (object sender, EventArgs e)
{
String str1 = null;

XmlDocument myxml = new XmlDocument ();
Myxml. Load (xmlPath );

XmlNode xn = myxml. SelectSingleNode ("bookstore ");

XmlNodeList xnl = xn. ChildNodes;

Foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement) xnf;
Str1 + = xe. GetAttribute ("genre ");
Str1 + = Environment. NewLine;
Str1 + = xe. GetAttribute ("ISBN ");
Str1 + = Environment. NewLine;

XmlNodeList xnf1 = xe. ChildNodes;
Foreach (XmlNode xn2 in xnf1)
{

Str1 + = xn2.InnerText;
Str1 + = Environment. NewLine;

}
}
MessageBox. Show (str1 );

}

* ****** Void Form1_Load (object sender, EventArgs e)
{
WebBrowser1.Navigate (xmlPath );

}

}
}
 

Source code download

 

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.