using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using Debug = System.Console;
public static partial class XmlReaderExtensions
public static IEnumerable<string> ReadAllElementContentsAsString(this XmlReader reader, string localName, string namespaceURI)
while (reader.ReadToFollowing(localName, namespaceURI))
yield return reader.ReadElementContentAsString();
public static IEnumerable<XmlReader> ReadAllSubtrees(this XmlReader reader, string localName, string namespaceURI)
while (reader.ReadToFollowing(localName, namespaceURI))
using (var subReader = reader.ReadSubtree())
public static class TestClass
<Name>documentClass</Name>
<value format="""" valueset=""{rechnung}"" originalValue=""Rechnung"" start=""0"" end=""0"" LD=""0"" doc=""C:\b4enviam-service-test\inputDir\031a0933-2616-4d8e-8a79-56746ae0e160/Invoice_51029062.pdf"">Rechnung</value>
<EntityType>Class</EntityType>
<precondition type=""optional"">All</precondition>
<value format="""" valueset="""" originalValue=""4352020616"" start=""0"" end=""0"" LD=""0"" doc=""C:\b4enviam-service-test\inputDir\031a0933-2616-4d8e-8a79-56746ae0e160/Invoice_51029062.pdf"">4352020616</value>
<EntityType>KB.GTInovice</EntityType>
<precondition type=""optional"">all</precondition>
<value format="""" valueset="""" originalValue="""" start=""0"" end=""0"" LD=""0"" doc="""">
<EntityType>KB.GTInovice</EntityType>
<precondition type=""optional"">all</precondition>
<Name>Should Not See Me For Nested Read</Name>
<value format="""" valueset=""{rechnung}"" originalValue=""Rechnung"" start=""0"" end=""0"" LD=""0"" doc=""C:\b4enviam-service-test\inputDir\031a0933-2616-4d8e-8a79-56746ae0e160/Invoice_51029062.pdf"">Rechnung</value>
<EntityType>Class</EntityType>
<precondition type=""optional"">All</precondition>
public static void Test()
var xmlNoSpace = XElement.Parse(xml).ToString(SaveOptions.DisableFormatting);
var xmlWithSpace = XElement.Parse(xml).ToString(SaveOptions.None);
Console.WriteLine("\nTesting original XML:");
Console.WriteLine("\nTesting XML without whitespace:");
Console.WriteLine("\nTesting XML with whitespace:");
Console.WriteLine("Tests passed.");
private static void Test(string xml)
Debug.WriteLine("Simple read:");
var list1 = new List<string>();
using (var textReader = new StringReader(xml))
using (var reader = XmlReader.Create(textReader))
foreach (var name in reader.ReadAllSubtrees("InformationTuple", "")
.Select(r => r.ReadAllElementContentsAsString("Name", "").First()))
Debug.WriteLine(" " + name);
Assert.IsTrue(XElement.Parse(xml).XPathSelectElements("//InformationTuple//Name").Select(e => e.Value).SequenceEqual(list1));
var list2 = new List<string>();
Debug.WriteLine("Read, <Name> optional:");
using (var textReader = new StringReader(xml))
using (var reader = XmlReader.Create(textReader))
foreach (var name in reader.ReadAllSubtrees("InformationTuple", "")
.SelectMany(r => r.ReadAllElementContentsAsString("Name", "").Take(1)))
Debug.WriteLine(" " + name);
Assert.IsTrue(list2.SequenceEqual(list1));
Debug.WriteLine("Read all names:");
var list3 = new List<string>();
using (var textReader = new StringReader(xml))
using (var reader = XmlReader.Create(textReader))
foreach (var name in reader.ReadAllSubtrees("InformationTuple", "")
.SelectMany(r => r.ReadAllElementContentsAsString("Name", "")))
Debug.WriteLine(" " + name);
Assert.IsTrue(list3.SequenceEqual(list1));
Debug.WriteLine("Nested Read:");
var list4 = new List<string>();
using (var textReader = new StringReader(xml))
using (var reader = XmlReader.Create(textReader))
foreach (var name in reader.ReadAllSubtrees("InformationTuples", "")
.SelectMany(r => r.ReadAllSubtrees("InformationTuple", ""))
.Select(r => r.ReadAllElementContentsAsString("Name", "").First()))
Debug.WriteLine(" " + name);
Assert.IsTrue(XElement.Parse(xml).XPathSelectElements("//InformationTuples//InformationTuple//Name").Select(e => e.Value).SequenceEqual(list4));
Debug.WriteLine("done.");
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];