2010-03-31 134 views
0

我有一個XML文檔如下:遞歸解析XMLDOCUMENT

<directory> 
<file><monitored>0</monitored> 
<xferStatus>1</xferStatus> 
<name>test1.txt</name> 
<size>7</size> 
<created>03/31/10 11:30:02 AM</created> 
<modified>03/31/10 11:30:00 AM</modified> 
<tPathList><tPath>http://hwcdn.net/p2f4h2b5/cds/testing/test1.txt</tPath> 
</tPathList> 
<tPath>http://hwcdn.net/p2f4h2b5/cds/testing/test1.txt</tPath> 
<oPathList><oPath>http://hwcdn.net/p2f4h2b5/w9m3i4q9/test1.txt</oPath> 
</oPathList> 
<oPath>http://hwcdn.net/p2f4h2b5/w9m3i4q9/test1.txt</oPath> 
<aPath></aPath> 
</file> 

<file><monitored>0</monitored> 
<xferStatus>1</xferStatus> 
<name>GenericDAO.cs</name> 
<size>1843</size> 
<created>03/31/10 11:41:10 AM</created> 
<modified>03/31/10 11:41:10 AM</modified> 
<tPathList><tPath>http://hwcdn.net/p2f4h2b5/cds/testing/GenericDAO.cs</tPath> 
</tPathList> 
<tPath>http://hwcdn.net/p2f4h2b5/cds/testing/GenericDAO.cs</tPath> 
<oPathList><oPath>http://hwcdn.net/p2f4h2b5/w9m3i4q9/GenericDAO.cs</oPath> 
</oPathList> 
<oPath>http://hwcdn.net/p2f4h2b5/w9m3i4q9/GenericDAO.cs</oPath> 
<aPath></aPath> 
</file> 
<nEntries>2</nEntries> 
</directory> 

那麼有文檔中的兩個文件,我怎麼可以遞歸地或迭代得到的文件,大小等..?

的反應是字符串格式,並且轉換成XML如下:

XmlTextReader textReader = new XmlTextReader(hwresponse); 
+0

你問一個問題在這裏?什麼是「迴應」? – Oded 2010-03-31 17:56:32

回答

5

嘗試的LINQ to XML(System.Xml.Linq的命名空間。)例:

XDocument document = XDocument.Parse(xml); // xml string 
var query = from file in document.Descendants("file") 
      select new 
       { 
        Monitored = (int)file.Element("monitored"), 
        Name = (string)file.Element("name"), 
        Size = (int)file.Element("size") 
       }; 

foreach (var file in query) 
{ 
    Console.WriteLine("{0}\t{1}", file.Name, file.Size); 
}