(C#) 操作XML之遍历

XML内容是个树结构,用C#读取XML里的结点(Node)的时候,首先要理解在.NET的世界里,什么是结点(Node)?
有如下XML:

<?xml version="1.0" encoding="utf-8" ?>

<bookstore>

  <book category="COOKING">

    <title lang="en">Everyday Italian</title>

    <author>Giada De Laurentiis</author>

    <year>2005</year>

    <price>30.00</price>

  </book>

  <book category="CHILDREN">

    <title lang="en">Harry Potter</title>

    <author>J K.Rowling</author>

    <year>2005</year>

    <price>29.99</price>

  </book>

  <book category="WEB">

    <title lang="en">Learning XML</title>

    <author>Erik T.Ray</author>

    <year>2003</year>

    <price>39.95</price>

  </book>

</bookstore>

其中: <?xml version="1.0" encoding="utf-8" ?> 是结点,<bookstore>是结点,category="COOKING"是结点,Everyday Italian也是结点。
查看MSDN的 XmlNodeType 枚举便会知道详细的情况: http://msdn.microsoft.com/zh-cn/library/system.xml.xmlnodetype.aspx

Node XmlNodeType
<?xml version="1.0" encoding="utf-8" ?> XmlDeclaration
<bookstore> Element
<book category="COOKING"> Element
category="COOKING" Attribute
<title lang="en">Everyday Italian</title> Element
Everyday Italian Text

所以,如果想获取所有的书详细信息(title,author,year,price),那么就需要遍历所有的Text结点。

        private void TraverseTextNodeInnerText(XmlNode node)

        {

            if (node.HasChildNodes)

            {

                XmlNodeList nodeList = node.ChildNodes;

                foreach (XmlNode nd in nodeList)

                {

                    if (nd.NodeType == XmlNodeType.Text)  

                    {

                        _TextNodeInnerTexts.Add(nd.InnerText);

                    }

                    TraverseTextNodeInnerText(nd); 

                }

            }

        }



        public string[] GetAllTextNodeInnerText(string node)

        {

            XmlDocument doc = XMLLoad();

            XmlNode nd = doc.SelectSingleNode(node);

            TraverseTextNodeInnerText(nd);

            return this._TextNodeInnerTexts.ToArray(); 

        }

 输出如下:

Everyday Italian

Gida De Laurentiis

2005

30.00

Harry Potter

J K.Rowling

2005

29.99

Learning XML

Erik T.Ray

2003

39.95

用LINQ to XML 也可以简单的对XML进行操作。 

你可能感兴趣的:(xml)