csharp XmlTextReader read xml file

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

 

posted @ 2012-01-11 20:22  ®Geovin Du Dream Park™  阅读(524)  评论(0编辑  收藏  举报