xml file:
<?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>
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());
分享到:
相关推荐
if (File.Exists("student.xml")) { reader = new XmlTextReader("student.xml"); } else { return; } int count = 0; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader...
using (var reader = new XmlTextReader("path_to_your_xml_file.xml")) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { Console.WriteLine($"Element: {reader.Name}"); } else ...
5. **XML文本读写器(XmlTextReader/XmlTextWriter)**:这些类提供了流式处理XML的能力,适合处理大型XML文件,因为它们不需要将整个文件加载到内存中。 ```csharp using (XmlTextReader reader = new ...
XmlTextReader reader = new XmlTextReader("file.xml"); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: Console.WriteLine("Element: {0}", reader.Name); break; // 处理...
在C#中,读取XML文件通常有两种方式:使用XmlTextReader或使用 XmlDocument。 1. 使用XmlTextReader: ```csharp using System; using System.Xml; public class Program { public static void Main() { ...
using (XmlTextReader textReader = new XmlTextReader("path_to_your_xml_file.xml")) { while (textReader.Read()) { // 处理每个节点 } } ``` 三、XML解析的注意事项 - 错误处理:读取XML时,可能遇到...
在C#中,我们通常使用`XmlTextReader`(`System.Xml`命名空间中的旧版API)或更现代的`XmlReader`类来读取XML文件。以下是一个使用`XmlReader`的简单示例: ```csharp using System; using System.IO; using System...
doc.Load("path_to_xml_file.xml"); ``` 3. **XML节点操作** `XmlNode`接口是所有XML节点的基类,包括元素、属性、注释等。可以使用`SelectNodes`或`SelectSingleNode`方法根据XPath表达式查找节点,然后对找到...
对于`XmlTextReader`,你可以使用`Read()`方法来遍历XML树,然后检查`NodeType`属性以确定当前节点的类型(元素、文本、注释等),并访问`Name`属性获取元素名: ```csharp while (reader.Read()) { if (reader....
XmlTextReader reader = new XmlTextReader(Server.MapPath("students.xml")); // 创建 ArrayList 存储节点值 ArrayList NodeValues = new ArrayList(); while (reader.Read()) { if (reader.NodeType == ...
- `XmlTextValidatingReader`:扩展了`XmlTextReader`,添加了验证功能,可以检查XML文档是否符合指定的XML Schema(XSD)。 - `XmlNodeReader`:允许将`XmlReader`的行为与`XmlNode`接口相匹配,方便那些习惯于使用...
using (System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myXmlFile, Encoding.UTF8)) { myXmlWriter.Formatting = Formatting.Indented; myXmlWriter.Indentation = 4; myXmlWriter....
while (xml.Read()) { if (xml.Name == "UpdateTime") { // 获取更新时间 LastUpdateTime = xml.GetAttribute("Date"); break; } } } } return LastUpdateTime; } ``` ### 结论 通过上述介绍可以看出...
File.Exists(path)) { // 处理文件不存在的情况 return null; } StringReader StrStream = null; XmlTextReader Xmlrdr = null; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); // ...
public static DataTable ReadFromXml(string address) { DataTable dt = new DataTable(); try { // 创建XmlTextReader对象 using (var reader = new XmlTextReader(address)) { while (reader.Read()) { ...
using (XmlTextReader reader = new XmlTextReader(fileStream)) { while (reader.Read()) { if (reader.Name == "UpdateTime") { lastUpdateTime = reader.GetAttribute("Date"); break; } } } } ...