[Fork] https://stackoverflow.com/questions/70041937/problem-parsing-through-multiple-elements-in-the-xunit-xml-file-using-xelement

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Reflection;
using System.Text.RegularExpressions;

using NUnit.Framework; //NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/70041937/problem-parsing-through-multiple-elements-in-the-xunit-xml-file-using-xelement

	public class TestResults
	{
		public TestResults(string result, string message) => (Result, Message) = (result, message);
		public string Result { get; }
		public string Message { get; }
	}

    public class TestClass
    {
		private static List<TestResults> GetTestAutomationExecutionResult(string filePath)
			=> GetTestAutomationExecutionResult(XElement.Load(filePath));

		private static List<TestResults> GetTestAutomationExecutionResult(XElement xelement)
		{
			var query = from e in xelement.Descendants()
				where e.Name.LocalName == "test"
				let r = e.Attribute("result").Value
				let m = r == "Fail" ? e.Elements("failure").Elements("message").FirstOrDefault()?.Value : ""
				select new TestResults(r, m);
			return query.ToList();
		}
		
        public static void Test()
        {
			var xelement = XElement.Parse(GetXml());
			Console.WriteLine(xelement);
			Console.WriteLine();
			var list = GetTestAutomationExecutionResult(xelement);
			
			foreach (var item in list)
			{
				Console.WriteLine("{0}: {1}", item.Result, item.Message);
			}
        }
		
		static string GetXml() => @"<?xml version=""1.0"" encoding=""utf-8""?>
<assemblies timestamp=""07/31/2018 14:58:48"">
    <assembly name=""C:\Users\bf\Documents\Visual Studio 2015\Projects\xUnitDemo\xUnitDemo\bin\Debug\xUnitDemo.DLL"" environment=""64-bit .NET 4.0.30319.42000 [collection-per-class, parallel (1 threads)]"" test-framework=""xUnit.net 2.3.1.3858"" run-date=""2018-07-31"" run-time=""14:58:47"" config-file=""C:\Users\bf\Documents\Visual Studio 2015\Projects\xUnitDemo\packages\xunit.runner.console.2.4.0\tools\net452\xunit.console.exe.Config"" total=""15"" passed=""14"" failed=""1"" skipped=""0"" time=""0.257"" errors=""0"">
        <errors />
        <collection total=""2"" passed=""1"" failed=""1"" skipped=""0"" name=""Test collection for xUnitDemo.SimpleTests"" time=""0.070"">
            <test name=""xUnitDemo.SimpleTests.PassingTest"" type=""xUnitDemo.SimpleTests"" method=""PassingTest"" time=""0.0636741"" result=""Pass"">
                <traits>
                    <trait name=""test"" value=""test"" />
                    <trait name=""requirement"" value=""test"" />
                    <trait name=""labels"" value=""test"" />
                </traits>
            </test>
            <test name=""xUnitDemo.SimpleTests.FailingTest"" type=""xUnitDemo.SimpleTests"" method=""FailingTest"" time=""0.0059474"" result=""Fail"">
                <failure exception-type=""Xunit.Sdk.EqualException"">
                   <message><![CDATA[Assert.Equal() Failure\r\nExpected: 5\r\nActual: 4]]></message>
                    <stack-trace><![CDATA[ at xUnitDemo.SimpleTests.FailingTest() in C:\Users\smsf\documents\visual studio 2015\Projects\xUnitDemo\xUnitDemo\SimpleTests.cs:line 30]]></stack-trace>
                </failure>
            </test>
        </collection>
    </assembly>
</assemblies>";
    }

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
		
		public static string GetNetCoreVersion()
		{
			//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
			//
			var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
			var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
			int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
			if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
				return assemblyPath[netCoreAppIndex + 1];
			return null;
		}
	}
;