XML 读取xml文件的各个节点
1,读取整个xml文件,包括子元素,文本,属性。
代码如下:
View Code
1 using System;
2 using System.Xml;
3
4 namespace ReadXMLfromFile
5 {
6 /// <summary>
7 /// Summary description for Class1.
8 /// </summary>
9 class Class1
10 {
11 static void Main(string[] args)
12 {
13 XmlTextReader reader = new XmlTextReader ("books.xml");
14 while (reader.Read())
15 {
16 switch (reader.NodeType)
17 {
18 case XmlNodeType.Element: // The node is an element.
19 Console.Write("<" + reader.Name);
20 Console.WriteLine(">");
21 break;
22 case XmlNodeType.Text: //Display the text in each element.
23 Console.WriteLine (reader.Value);
24 break;
25 case XmlNodeType.EndElement: //Display the end of the element.
26 Console.Write("</" + reader.Name);
27 Console.WriteLine(">");
28 break;
29 }
30 }
31 Console.ReadLine();
32 }
33 }
34 }
2 using System.Xml;
3
4 namespace ReadXMLfromFile
5 {
6 /// <summary>
7 /// Summary description for Class1.
8 /// </summary>
9 class Class1
10 {
11 static void Main(string[] args)
12 {
13 XmlTextReader reader = new XmlTextReader ("books.xml");
14 while (reader.Read())
15 {
16 switch (reader.NodeType)
17 {
18 case XmlNodeType.Element: // The node is an element.
19 Console.Write("<" + reader.Name);
20 Console.WriteLine(">");
21 break;
22 case XmlNodeType.Text: //Display the text in each element.
23 Console.WriteLine (reader.Value);
24 break;
25 case XmlNodeType.EndElement: //Display the end of the element.
26 Console.Write("</" + reader.Name);
27 Console.WriteLine(">");
28 break;
29 }
30 }
31 Console.ReadLine();
32 }
33 }
34 }