如何在C#中读取XML文件?

时间:2015-01-12 10:56:54

标签: c# xml

假设我有一个XML文件,如下所示:

<TEST>
 <RESULT>
    <REPORT_CREATION_TIME> 15:22:34 </REPORT_CREATION_TIME>
    <TEST_START_TIME> 15:22:34 </TEST_START_TIME>
    <TEST_END_TIME> 15:22:54 </TEST_END_TIME>
    <ERRORS_FOUND> 0 </ERRORS_FOUND>
 </RESULT>
</TEST>

我想在控制台中打印以下内容:

REPORT_CREATION_TIME = 15:22:34 // node name and value
TEST_START_TIME = 15:22:34
TEST_END_TIME = 15:22:54
ERRORS_FOUND = 0

我不想使用LINQ语句。怎么做?

我目前正在

XmlDocument doc = new XmlDocument();
doc.Load("C:\\TEST.xml");
XmlNode node = doc.SelectSingleNode("TEST/RESULT");

2 个答案:

答案 0 :(得分:0)

XmlNode node = doc.SelectSingleNode("TEST/RESULT");
foreach(XmlNode child in node.ChildNodes)
{
    Console.WriteLine("{0} = {1}", child.Name, child.Value.Trim());
}

喜欢这个吗?

答案 1 :(得分:0)

你可以试试这个

         XmlNodeList oList = oDoc.SelectNodes("TEST/RESULT");
            foreach (XmlNode oNode in oList)
            {
                 string name = oNode.Name;
                 string Value = oNode.Value;
//Do whatever you want
            }