csharp XmlTextReader read xml file

本文介绍了一种使用C#解析XML文件的方法,并统计了不同类型的XML节点数量,包括元素、属性、注释等。通过遍历XML文档,该方法能够有效地获取XML结构中的关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



XmlException: Attribute name and qualified name must be identical. file:///D:/Lua_Dating538/Assets/Plugins/Android/AndroidManifest.xml Line 3, position 61. Mono.Xml2.XmlTextReader.ReadStartTag () Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) UnityEditor.AndroidXmlDocument..ctor (System.String path) UnityEditor.AndroidManifest..ctor (System.String path) UnityEditor.Android.PostProcessAndroidPlayer.CopyMainManifest (System.String target, System.String playerPackage) UnityEditor.Android.PostProcessAndroidPlayer.PostProcessInternal (System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args) UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:316) UnityEditor.HostView:OnGUI()
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值