开发者

how to read and write data from multinode xml

开发者 https://www.devze.com 2023-02-20 07:12 出处:网络
how to read this kind of xml where there are multinodes. The format of the xml is: <testresultdata>

how to read this kind of xml where there are multinodes. The format of the xml is:

<testresultdata>
  <testsetup>
    <testID>1</testID>
        <freqiency>80</freqiency>
        <level>1</level>
        <application>
              <appID>1</appID>
              <result>Pass</result>
        </application>
        <application>
              <appID>2</appID>
              <result>Fail</result>
        </applicati开发者_JS百科on>
  </testsetup>
</testresultdata>

thanks in advance.


It's not really clear what you mean... if you want to read all the application elements, for example, you could use:

XDocument doc = XDocument.Load("test.xml");
var query = doc.Descendants("application")
               .Select(x => new { AppID = (int) x.Element("appID"),
                                  Result = (string) x.Element("result") })
               .ToList();


A couple simple LINQ to XML examples:

    XDocument document = XDocument.Load("test.xml");
    var level = document.Descendants("testsetup")
                        .Where(x => x.Element("testID").Value == "1")
                        .Select(x => x.Element("level").Value)
                        .Single();


    var results = document.Descendants("application")
                          .Elements("result")
                          .Select(x => x.Value)
                          .ToList();

The first one reads the single value of level for testsetup with testId 1, the second gets you the results of all applications.

0

精彩评论

暂无评论...
验证码 换一张
取 消