【问题标题】:xmlreader traversing nodesxmlreader 遍历节点
【发布时间】:2012-08-08 19:19:24
【问题描述】:

我有一个xml文件如下,

<rss> 
<report name="rpt1"> 
<title>AAA</title> 
<image/> 
<weblink/> 
<pdflink/> 
<pdfsize/> </report> 
<report name="rpt2"> 
<title>BBB</title> 
<image/> 
<weblink/> 
<pdflink/> 
<pdfsize/> </report> 
</rss>

我必须遍历链接并转到报告节点并获取每个报告的标题/图像/weblink/pdflink/pdfsize。我如何使用 xml 阅读器做到这一点。我谷歌并看到遍历单个节点但不是循环。有输入吗?

【问题讨论】:

    标签: asp.net .net xmlreader


    【解决方案1】:

    您可以使用LINQtoXML 从您的 XML 中获取项目。

    var path = Server.MapPath("~/Content/pairs.xml");    
    XElement elm = XElement.Load(path);
    //you can also load the XML from stream / string also
    
    if (elm != null)
    {
        foreach (var item in elm.Elements("report"))
        {
            string title = item.Element("title").Value;
            string image = item.Element("image").Value;
            string weblink= item.Element("weblink").Value;
            //do whatever with the values          
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多