我有这个xml文件:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
<test-results name="<path>" total="1" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2014-08-12" time="16:05:41">
<environment nunit-version="2.6.3.13283" (...)
(...)
</test-results>
我希望在程序“总计”,“错误”等中获得总计,错误的整数值。我怎样才能得到这个值?
答案 0 :(得分:1)
尝试以下代码: -
var xDoc = XDocument.Load("XMLFile1.xml");
foreach (var elem in xDoc.Document.Descendants("test-results"))
{
var total = int.Parse(elem.Attribute("total").Value);
var error = int.Parse(elem.Attribute("errors").Value);
}
答案 1 :(得分:1)
这个
XDocument xDoc = XDocument.Load(@"your path");
int total=0;
foreach (var elem in xDoc.Document.Descendants("test-results"))
{
total += int.Parse(elem.Attribute("total").Value);
}
答案 2 :(得分:0)
使用以下内容:
//path is the path to your xml file
XmlTextReader reader = new XmlTextReader(path);
XmlDocument doc = new XmlDocument();
XmlNode node = doc.ReadNode(reader);
foreach (XmlNode chldNode in node.ChildNodes)
{
//Read the attribute Name
if (chldNode.Name.ToString().Equals("test-results"))
{
int total = Int32.Parse(chldNode.Attributes["total"].Value.ToString());
}
}