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();

    }

输出:
C# 使用XmlTextReader读取XML文件

上一篇:Mybatis plus强大的条件构造器QueryWrapper条件构造器基础方法解释


下一篇:mybatis分页查询(个人笔记,非教程)