2012-08-09 25 views
2

如何從xml文檔獲取信息? 我在c:\ temp \ data.xml有一個xml文檔,並使用visual studio。當我知道確切的路徑時,如何使用c#查找XML元素?

我可以計算最接近的是:

XmlDocument xdoc = new XmlDocument(); 
xdoc.Load(@"C:\temp\data.xml"); 
date = xdoc.SelectSingleNode("/forcast_informat… 

XML文檔看起來是這樣的:

<?xml version="1.0"?> 
-<xml_api_reply version="1"> 
    -<weather section="0" row="0" mobile_zipped="1" mobile_row="0" tab_id="0" module_id="0"> 
     -<forecast_information> 
      etc etc... 
      <current_date_time data="2012-08-09 21:53:00 +0000"/> 
      etc, etc... 

所有我想要做的就是搶2012-08-09 21:53這個日期:00 +0000 ...有什麼建議?

回答

5

這應該做的伎倆:

XmlDocument xdoc = new XmlDocument(); 
xdoc.Load(@"C:\temp\data.xml"); 
XmlNode dataAttribute = xdoc.SelectSingleNode("/xml_api_reply/weather/forecast_information/current_date_time/@data"); 

Console.WriteLine(dataAttribute.Value); 
0

試試這個。這將爲每個預測加載當前日期和時間:

XmlDocument XMLDoc = new XmlDocument(); 
XMLDoc.Load(XMLDocumentPath); 
XmlNodeList NodeList = XMLDoc.SelectNodes("/xml_api_reply/weather/forecast_information/"); 
foreach(XmlNode Node in NodeList) 
{ 
string DTime = Node["current_date_time"].InnerText; 
//Do something with DTime 
} 
相關問題