Method for reading and writing the contents of an XML file and displaying it on an ListView control
- 2021-12-04 19:28:56
- OfStack
XML files consist of content and tags. Include a large part of the content in the element by tagging around the content [1]. XML nodes are convenient for flexible expansion of program configuration, especially when configuration information in array format exists, adding rows or columns only needs to modify XML files without modifying source code.
1. Introduction to the XML document
(1) Statement
Line 1 of an XML document can be an XML declaration [1] This is an optional part of the file that identifies the file as an XML file and helps tools and humans recognize XML (not mistaken for SGML or other tags). This statement can be simply written as < ?xml? > Or contains the XML version < ?xml version="1.0"? > And even includes character encoding, such as for Unicode < ?xml version="1.0" encoding="utf-8"? > .
(2) Root Node Creation
The start and end tags of the root node are used to surround the contents of the XML document. A file can only have 1 root node and need to include it using "wrapper" [1].
(3) Child nodes
There is no limit to the number of word nodes, and child nodes can also be nested in child nodes. The smallest node is the marked content.
<?xml version="1.0" encoding="utf-8"?>
<UIList>
<column>
<colname>
<colvalue>55</colvalue>
</colname>
<colname>
<colvalue>85</colvalue>
</colname>
</column>
<row>
<rowvalue>
<columnvalue>1</columnvalue>
<columnvalue>9</columnvalue>
</rowvalue>
<rowvalue>
<columnvalue>2</columnvalue>
<columnvalue>9</columnvalue>
</rowvalue>
</row>
</UIList>
2. Read the contents of an XML file and display it on an ListView control
The logic of XML file reading is to start from the root node, find the tag array according to the tag, and then traverse the tag array to read out the corresponding value. To make the real results more intuitive, display them on the ListView control mentioned in the previous article.
(1) The first step is to import the namespace. There is XML class in this namespace, through which XML file can be read.
using System.Xml;
(2) Load the xml file
XmlDocument doc = new XmlDocument();
doc. Load ("UIConfig. xml"); //Load the Xml file
(3) Get the root node
XmlElement rootElem = doc.DocumentElement;
(4) Get an array of nodes
An array of nodes is based on the root node, where its contents are typed into the array according to tags.
XmlNodeList ColumnpersonNodes = rootElem.GetElementsByTagName("colname");
(5) Traverse the node array and display the value on ListView
private void Form1_Load(object sender, EventArgs e)
{
//ListView
listView1.View = View.Details;
//XML
// Loading xml Documents
XmlDocument doc = new XmlDocument();
doc.Load("UIConfig.xml"); // Loading Xml Documents
XmlElement rootElem = doc.DocumentElement; // Get the root node
XmlNodeList ColumnpersonNodes = rootElem.GetElementsByTagName("colname"); // Gets an array of column nodes
foreach (XmlNode node in ColumnpersonNodes)
{
// Writes column data to the ListView Medium
XmlNodeList subNodes = ((XmlElement)node).GetElementsByTagName("colvalue");
foreach (XmlNode subNode in subNodes)
{
// Write row data to the ListView Medium
string strWidth = subNode.InnerText;
int Width = Convert.ToInt32(strWidth);
listView1.Columns.Add(strWidth, Width, HorizontalAlignment.Center);
}
}
XmlNodeList RowpersonNodes = rootElem.GetElementsByTagName("rowvalue"); // Get an array of row nodes
foreach (XmlNode node in RowpersonNodes)
{
ListViewItem lvi = new ListViewItem();// Open up row data space
XmlNodeList subNodes = ((XmlElement)node).GetElementsByTagName("columnvalue");
lvi.SubItems[0].Text = subNodes[0].InnerText;// Will be 1 A value is given to the outfit
int Count = subNodes.Count;
for (int i = 1; i < Count; i++)// Traversal number 1 A value other than a value
{
// Write row data to the ListView Medium
string str = subNodes[i].InnerText;
lvi.SubItems.Add(str);
}
listView1.Items.Add(lvi);
}
}
3. Write an XML file
XML file write and read the logic of 1 sample, starting from the root node, according to the tag to find the tag array, and then traverse the tag array, modify the corresponding value, and finally save.
Add a shutdown event to Form1, in which XML modifications are implemented.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("UIConfig.xml"); // Loading Xml Documents
// Get listview Existing column size
XmlElement rootElem = doc.DocumentElement; // Get the root node
XmlNodeList ColumnpersonNodes = rootElem.GetElementsByTagName("colvalue"); // Get person Child node set
for(int i = 0; i < this.listView1.Columns.Count; i++)
{
// Writes the existing column size to the xml Documents
int colWidth = this.listView1.Columns[i].Width;// Every 1 Actual width of column
// Writes column data to the ListView Medium
ColumnpersonNodes[i].InnerText = colWidth.ToString();
doc.Save("UIConfig.xml");
}
}