2012-08-09 68 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 
} 
相关问题