C# 使用XmlTextReader读取XML文件
作者:互联网
测试使用XmlTextReader 读写XML文件的内容。
测试XML文件 Products.xml:
<Product_id>1</Product_id>
<Product_name>Product 1</Product_name>
<Product_price>1000</Product_price>
<Product_id>2</Product_id>
<Product_name>Product 2</Product_name>
<Product_price>2000</Product_price>
<Product_id>3</Product_id>
<Product_name>Product 3</Product_name>
<Product_price>3000</Product_price>
<Product_id>4</Product_id>
<Product_name>Product 4</Product_name>
<Product_price>4000</Product_price>
C# 读取代码:
using System.Xml;
static void Main(string\[\] args)
{
String URLString = "Products.xml";
XmlTextReader reader = new XmlTextReader(URLString);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read the attributes.
{
Console.Write(" " + reader.Name + "='" + reader.Value + "'");
Console.Write(">");
}
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
输出:
标签:XML,Product,Console,C#,reader,XmlNodeType,XmlTextReader 来源: https://blog.csdn.net/flysh05/article/details/122029838