5

xml ドキュメントから情報を取得するにはどうすればよいですか? c:\temp\data.xml に xml ドキュメントがあり、Visual Studio を使用しています。

私が把握できる最も近いものは次のとおりです。

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 の日付を取得することだけです...何か提案はありますか?

4

2 に答える 2

11

これでうまくいくはずです:

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);
于 2012-08-09T23:45:47.033 に答える
2

これを試して。これにより、すべての予測の現在の日付と時刻が読み込まれます。

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
}
于 2012-08-09T23:52:37.730 に答える