First, let's take a brief look at XML
Extensible Markup Language XML (extensible Markup Language), is developed by the organization of the Consortium. As a new markup language for the substitution of HTML language, XML has many basic standards within it.
XML is applied to scientific computing, electronic publishing, multimedia production and e-commerce through the combination of these relevant standards.
C # and XML instances:
(i) Attempt to create an XML file
(1). First create an empty XML document:
There is a class "XmlDocument" in the Namespace "System.Xml", in which C # describes the XML document. The following is an XML document created in C #.
xmldoc = new System.Xml.XmlDocument ();
(2). Add the XML declaration paragraph at the head of the XML document:
Using the "CreateNode" method in the "XmlDocument" class to create an XML node of the specified type, there are three methods of calling the "CreateNode" method, one of the first methods used in this article, with the following syntax:
Xmldocument.createnode Method (XmlNodeType, String, String)
Then, using the "AppendChild" method in the "XmlDocument" class to add this node to the XML document, the following statements are included in the XML document with the declaration paragraph implementation in C #:
XmlNode = xmldoc. CreateNode (Xmlnodetype.xmldeclaration, "", "");
XmlDoc. AppendChild (XmlNode);
(3) adding elements (element) to an XML document:
The addition of the data content is through elements, which provide two methods in the "XmlDocument" class: "CreateElement" and "createTextNode". The first method is to create an element in XML, and the other is to specify a literal value for the element that is created. The following is a root element for the XML document created above.
Xmlelem = xmldoc. CreateElement ("", "ROOT", "");
XmlText = xmldoc. createTextNode ("Root Text");
Note: where "Xmlelem" is the "XmlElement" object created, "XmlText" is the "XmlText" object
With an example of creating an XML element, you can create additional data based on the different structure of the data in the XML document.
Run the program and generate the Data.xml file in the D drive
(ii) reading an XML file
(1) Re-write a copy of the XML file
In the Data.xml file, the contents are as follows:
(2) read in C #
<1> loading XML files to form data streams:
By creating a "XmlDocument" object, and then using the "load" method, you can load the XML file as follows:
XmlDocument doc = new XmlDocument ();
Loading the specified XML document
Doc. Load ("C:\\data.xml");
<2>. Read the XML file and display it:
The read XML is implemented by creating a "XmlNodeReader" object, and the "XmlNodeReader" object is primarily used to read the node data of the XML. Some "XmlNodeReader" properties, such as the "NodeType" attribute, are used in the program of this article to determine what type of read-get node is. "Value" is the value of the node. Here is the implementation code that reads the XML file and displays it in the ListView, where ListView1 is already creating the ListView component:
while (reader. Read ())
{
Determine the current read Get node type
Switch (reader. NodeType)
{
Case XmlNodeType.Element:
s = reader. Name;
break;
Case XmlNodeType.Text:
if (S.equals ("Name"))
MyItem = LISTVIEW1.ITEMS.ADD (reader. Value);
Else
MYITEM.SUBITEMS.ADD (reader. Value);
break;
}
}
To design the Windows Forms interface:
Add events or assignments for each space:
Main function (read XML file):
The results are as follows:
C#&&xml