csharp XmlTextReader read xml file

xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <UserInfo>
    <Login Name="geovindu">
      <Pass>l17pi6MYm2g=</Pass>
    </Login>
    <Login Name="涂聚文祝你龙年大悦http://www.dusystem.com/Happynewyear.html">
      <Pass>4IGT6o/qA4vKYzlgizWsDg==</Pass>
    </Login>
    <Login Name="涂聚文">
      <Pass>ULNR7dw9V7M=</Pass>
    </Login>
  </UserInfo>
</Root>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
int ws = 0;
 
 int pi = 0;
 
 int dc = 0;
 
 int cc = 0;
 
 int ac = 0;
 
 int et = 0;
 
 int el = 0;
 
 int xd = 0;
 XmlDocument xmlConditions = new XmlDocument();
  
 string url = Server.MapPath("User.xml");
 XmlTextReader textReader = new XmlTextReader(url);
 //textReader.WhitespaceHandling = WhitespaceHandling.None;
 //xmlConditions.Load(textReader);
 //XmlNode xnodDE = xmlConditions.DocumentElement;
 
 //Response.Write(xnodDE.ChildNodes[0].ToString());
 
// string t=ChildDisplay(xnodDE, 0);
 //Response.Write(t);
 //textReader.Close();
 string sName = "";
 while (textReader.Read())
 {
     XmlNodeType nType = textReader.NodeType;
 
     // If node type us a declaration
 
     if (nType == XmlNodeType.XmlDeclaration)
     {
 
         Response.Write("Declaration:" + textReader.Name.ToString() + "<br/>");
 
         xd = xd + 1;
 
     }
 
     // if node type is a comment
 
     if (nType == XmlNodeType.Comment)
     {
 
         Response.Write("Comment:" + textReader.Name.ToString() + "<br/>");
 
         cc = cc + 1;
 
     }
 
     // if node type us an attribute
 
     if (nType == XmlNodeType.Attribute)
     {
 
         Response.Write("Attribute:" + textReader.Name.ToString() + "<br/");
 
         ac = ac + 1;
 
     }
 
     // if node type is an element
 
     if (nType == XmlNodeType.Element)
     {
 
         Response.Write("Element:" + textReader.Name.ToString() + "<br/>");
 
         el = el + 1;
 
     }
 
 
 
     // if node type is an entity\
 
     if (nType == XmlNodeType.Entity)
     {
 
         Response.Write("Entity:" + textReader.Name.ToString() + "<br/>");
 
         et = et + 1;
 
     }
 
     // if node type is a Process Instruction
 
     if (nType == XmlNodeType.Entity)
     {
 
         Response.Write("Entity:" + textReader.Name.ToString() + "<br/>");
 
         pi = pi + 1;
 
     }
 
     // if node type a document
 
     if (nType == XmlNodeType.DocumentType)
     {
 
         Response.Write("Document:" + textReader.Name.ToString() + "<br/>");
 
         dc = dc + 1;
 
     }
 
     // if node type is white space
 
     if (nType == XmlNodeType.Whitespace)
     {
 
         Response.Write("WhiteSpace:" + textReader.Name.ToString() + "<br/>");
         //Response.Write("name:" + xmlConditions.Attributes["Name"].ToString());
         //Response.Write("login:" + xmlConditions.GetElementsByTagName("Login").ToString());
         ws = ws + 1;
 
     }
 
 
     switch (textReader.NodeType)
     {
 
         case XmlNodeType.Element:
             sName = textReader.Name;
             Response.Write("读取:Element:" + sName + "<br/>");
             if (textReader.HasAttributes)
             {
                 for (int i = 0; i <= textReader.AttributeCount - 1; i++)
                     Response.Write("屬性: " + textReader.GetAttribute(i) + " <br/>");
                 Response.Write("屬性Name: " + textReader.GetAttribute("Name") + " <br/>");
             }
             if (sName == "Login")
             {
                 Response.Write("IF屬性Name: " + textReader.GetAttribute("Name") + " <br/>");
             }
             break;
         case XmlNodeType.Text:
             switch (sName)
             {
                 case "Login":
                     Response.Write("Login:"+textReader.Value+"<br/>");
                     if (textReader.HasAttributes)
                     {
                         for (int i = 0; i <= textReader.AttributeCount - 1; i++)
                             Response.Write("屬性: " + textReader.GetAttribute(i) + " <br/>");
                     }
                     break;
                 case "Pass":
                     Response.Write("Pass:" + textReader.Value + "<br/>");
                     break;
                 case "UserInfo":
                     Response.Write("UserInfo:" + textReader.Value + "<br/>");
                     break;
             }
             break;
     }
     
 
 
 }
 
 Response.Write("Total Comments:" + cc.ToString());
 
 Response.Write("Total Attributes:" + ac.ToString());
 
 Response.Write("Total Elements:" + el.ToString());
 
 Response.Write("Total Entity:" + et.ToString());
 
 Response.Write("Total Process Instructions:" + pi.ToString());
 
 Response.Write("Total Declaration:" + xd.ToString());
 
 Response.Write("Total DocumentType:" + dc.ToString());
 
 Response.Write("Total WhiteSpaces:" + ws.ToString());

 

posted @   ®Geovin Du Dream Park™  阅读(524)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2012年1月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示