A brief introduction to reading XML files by C #
This article mainly explains the "brief introduction of C# reading XML file", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "C# read XML file brief introduction" bar!
C # reads the XML file:
After compiling NO2.cs, execution produces a "data.xml" file, where the data structure of the "data.xml" file is as follows:
Ma Jin Hu 239000 Feng Huang Street Chu Zhou City ANHUI Wang Tian 239000 Lang Ya Street He Fei City ANHUI Zou Wen Biao 100000 Sai Di Street Bei Jin City Bei Jin
In the following content, we use C # to read the XML file and use the ListView component to display it according to the data structure. The following figure shows the running interface after reading this XML.
To read XML data correctly, you must first understand the structure of XML.
1. Load the XML file to form a data stream.
You can load the XML file by creating a "XmlDocument" object and then using the "load" method, as follows:
XmlDocument doc = new XmlDocument (); / / load the specified XML document doc.Load ("C:\\ data.xml")
2.C# reads the XML file and displays:
In the program of this article, some "XmlNodeReader" attributes, such as "NodeType" attributes, are used to determine what type of read get node is. "Value" is the value of the node.
While (reader.Read ()) {/ / determine the current read acquisition 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 }} Thank you for your reading, the above is the content of "A brief introduction to reading XML files in C#". After the study of this article, I believe you have a deeper understanding of the problem of reading XML files in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!