xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<result name="response" numFound="4" start="0">
<doc>
<str name="id">testdoc</str>
</doc>
<doc>
<str name="author">hello</str>
<str name="id">123</str>
<arr name="title">
<str>123</str>
</arr>
</doc>
<doc>
<str name="author">world</str>
<str name="id">234</str>
<arr name="title">
<str>234</str>
</arr>
</doc>
<doc>
<str name="author">please</str>
<str name="id">345</str>
<arr name="title">
<str>345</str>
</arr>
</doc>
</result>请问如何将读取每个doc下面的author、id、title,请用C#,万分感谢!

解决方案 »

  1.   

    LINQ TO XML
    http://msdn.microsoft.com/en-us/library/bb387098.aspx
      

  2.   


                XDocument doc = XDocument.Load("c:\\1.txt");
                var vs = doc.Descendants("doc");
                foreach (var v in vs)
                {
                    foreach (XElement item in v.Nodes())
                    {
                        MessageBox.Show(item.Value);
                    }
                }
      

  3.   


    我需要遍历所有doc,但对于doc下面的author等值要准确定位,谢谢
      

  4.   


    void Main()
    {
    XDocument xml=XDocument.Load("c:\\test.xml");
    var query=from x in xml.Descendants("doc")  
     
      select new
    {
       author=x.Elements("str").Any(s=>s.Attribute("name").Value=="author")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="author").Value:"",
       id=x.Elements("str").Any(s=>s.Attribute("name").Value=="id")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="id").Value:"",
       title=x.Elements("str").Any(s=>s.Attribute("name").Value=="title")?x.Elements("str").FirstOrDefault(z=>z.Attribute("name").Value=="title").Value:"",
    };
        foreach(var item in query)
    Console.WriteLine("author:{0}\tid:{1}\ttitle:{2}",item.author,item.id,item.title);
    }
      

  5.   

    结果:author: id:testdoc title:
    author:hello id:123 title:
    author:world id:234 title:
    author:please id:345 title:
      

  6.   


     foreach (XElement r in XElement.Load(Server.MapPath("~/XMLFile1.xml")).Elements("doc"))
                {
                    var idobj=r.Elements("str").Where(c => c.Attribute("name").Value=="id");
                    var titleobj=r.Elements("arr").Where(c => c.Attribute("name").Value == "title");
                    var authorobj = r.Elements("str").Where(c => c.Attribute("name").Value == "author");
                    string id="", author="", title = "";
                    if (idobj.ToList().Count!= 0)  id = idobj.First().Value;
                    if (titleobj.ToList().Count != 0) title = titleobj.First().Value;
                    if (authorobj.ToList().Count != 0) author = authorobj.First().Value;                Response.Write("id:" + id + "titile:" + title + "author:" + author + "<br>");            }
      

  7.   

    结果
    id:testdoctitile:author:
    id:123titile:123author:hello
    id:234titile:234author:world
    id:345titile:345author:please
      

  8.   

    结构有点乱
    考虑下易读及操作性将xml结构稍稍改下,
    就很容易操作了,如:
    1.<?xml version="1.0" encoding="UTF-8"?>
    <result name="response" numFound="4" start="0">
      <doc id="testdoc">
        <author>hello</author>
        <id>123</id>
        <title>123</title>
      </doc>
      <doc id="testdoc2">
        <author>world</author>
        <id>234</id>
        <title>234</title>
      </doc>
      <doc id="testdoc3">
        <author>please</author>
        <id>345</id>
        <title>345</title>
      </doc>
    </result>2.
    <?xml version="1.0" encoding="UTF-8"?>
    <result name="response" numFound="4" start="0">
      <doc id="testdoc">
        <book author="hello" id="123" title="123"></book>
      </doc>
      <doc id="testdoc2">
        <book author="world" id="234" title="234"></book>
      </doc>
      <doc id="testdoc3">
        <book author="please" id="345" title="345"></book>
      </doc>
    </result>
      

  9.   

    XmlDocument doc = new XmlDocument();
    doc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "a.xml");
    XmlNodeList xmllist = doc.SelectSingleNode("result").ChildNodes;
    for (int i = 0; i < xmllist.Count; i++)
    {
         XmlNodeList c_xmllist = xmllist[i].ChildNodes;
         for (int j = 0; j < c_xmllist.Count; j++)
         {
             Console.WriteLine(c_xmllist[j].Attributes[0].Value + ":" + c_xmllist[j].InnerText);
         }
    }
    Console.ReadLine();
      

  10.   


    请问这个title怎么显示出来呢?
      

  11.   

    XElement.Load(Server.MapPath("~/XMLFile1.xml")).Elements("doc")这一句,我是用XElement xd = XElement.Load(streamReader);xd.Elements("doc"),但是进不了foreach循环
      

  12.   

    看4楼代码:Console.WriteLine("author:{0}\tid:{1}\ttitle:{2}",item.author,item.id,item.title);
      

  13.   

    我用xd.Elements("result").Elements("doc")就可以进行循环了,谢谢
      

  14.   


    不是这个意思,我的意思是这个title的变量是空的,所以显示不出来
      

  15.   

    我的XML第一个DOC标签内又没有title属性的标签,当然为空了