Reputation: 24385
I'm trying to parse an xml file generated by running unit tests, but the xml.Root.Elements()
is coming up as null.
Here a sample of the xml:
<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="2ece436d-907d-4f59-9c81-0544b2f4f7cd" name="redacted" runUser="redacted" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestSettings name="Default Test Settings" id="e00bb4af-1518-4b40-9905-19fa7b190f68">
...
</TestSettings>
<Times creation="2015-02-18T11:47:02.7188640-05:00" queuing="2015-02-18T11:47:03.1819103-05:00" start="2015-02-18T11:47:03.2819203-05:00" finish="2015-02-18T11:47:05.5571478-05:00" />
<ResultSummary outcome="Failed">
<Counters total="74" executed="74" passed="73" error="0" failed="1" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
</ResultSummary>
I want to retreive the <ResultSummary>
's outcome
, but all Element()
and Elements()
calls on xml.Root
are returning null.
var xml = XDocument.Load(resultFile);
var outcome = xml.Root
.Element("TestRun")
.Element("ResultSummary")
.Attribute("outcome")
.Value;
I tried adding the namespace like in this answer but I'm still getting the same results:
var xml = XDocument.Load(resultFile);
XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
var outcome = xml.Root
.Element(ns + "TestRun")
.Element("ResultSummary")
.Attribute("outcome")
.Value;
Also, I'm not sure if Root
is supposed to return <TestRun>
or not, I tried running xml.Root.Element("ResultSummary")
and I still get null.
How can I read this xml file?
Upvotes: 3
Views: 6756
Reputation: 2976
Other way to get the Namespace
is like this XNamespace ns = xml.Root.GetDefaultNamespace();
Upvotes: 2
Reputation: 26635
First of all, XDocument.Root
gets the root element of the XML Tree. And in your case it is TestRun
.
And, for getting object from a local name and a namespace, you can use XName.Get(string, string)
method:
So, change your code as:
string ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
var outcome = xml.Root
.Element(XName.Get("ResultSummary", ns))
.Attribute("outcome")
.Value;
The result is: Failed
Upvotes: 6