<?xml version="1.0" encoding="UTF-8" ?>
- <result>
<response>3</response>
- <sms>
<phone>1376612345678</phone>
<content>test</content>
<sendTime>2009-02-06 09:01:33.0</sendTime>
</sms>
- <sms>
<phone>1386612345678</phone>
<content>%E6%B5%8B%E8%AF%95</content>
<sendTime>2009-02-06 09:01:33.0</sendTime>
</sms>
- <sms>
<phone>1396612345678</phone>
<content>abc</content>
<sendTime>2009-02-06 09:01:33.0</sendTime>
</sms>
</result>上述内容保存到 string str ="";里
我想遍历出每个节点的phone跟content

解决方案 »

  1.   

    定义一个XMLDocument对象,初始化的字符串为将上面这段字符串。
    接下来就是C#中对XML节点的操作。这些操作网上大把大把的
      

  2.   

    就取<phone>  </phone>
    <content>  </content>
    的内容?
      

  3.   


    public void XMLTest()
            {
                string Str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
                Str=Str+" <result>";
                Str=Str+"<response>3</response>";
                Str=Str+"<sms>";
                Str=Str+"<phone>1376612345678</phone>";
                Str = Str + "<content>test</content>";
                Str=Str+"<sendTime>2009-02-06 09:01:33.0</sendTime>";
                Str=Str+"</sms>";
                Str=Str+"<sms>";
                Str=Str+"<phone>1386612345678</phone>";
                Str=Str+"<content>%E6%B5%8B%E8%AF%95</content>";
                Str=Str+"<sendTime>2009-02-06 09:01:33.0</sendTime>";
                Str=Str+"</sms>";
                Str=Str+"<sms>";
                Str=Str+"<phone>1396612345678</phone>";
                Str=Str+"<content>abc</content>";
                Str=Str+"<sendTime>2009-02-06 09:01:33.0</sendTime>";
                Str=Str+"</sms>";     
                Str=Str+"</result>";
                StringReader Reader = new StringReader(Str);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Reader);
                IList<String> Phones = new List<String>();
                IList<String> Contents = new List<String>();
                foreach (XmlNode xn0 in xmlDoc.ChildNodes)
                {
                    foreach (XmlNode xn1 in xn0.ChildNodes)
                    {
                        foreach (XmlNode xn2 in xn1.ChildNodes)
                        {
                            if (xn2.Name == "phone")
                            {
                                Phones.Add(xn2.InnerText.ToString());
                            }
                            if (xn2.Name == "content")
                            {
                                Contents.Add(xn2.InnerText.ToString());
                            }
                        }
                    }
                }        }
      

  4.   


    using System.Xml;
    using System.IO;
    //加上上面两个命名空间