xmlreader遍历节点

时间:2012-08-08 19:19:25

标签: asp.net .net xmlreader

我有一个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>

我必须遍历链接并转到报告节点,并为每个报告获取title / image / weblink / pdflink / pdfsize。我怎么能用xml阅读器做到这一点。我谷歌并看到遍历单个节点但不在循环中。任何输入?

1 个答案:

答案 0 :(得分: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          
    }
}
相关问题