questions/48778766/xdocument-parsing-error

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Xml.Linq;

    // https://stackoverflow.com/questions/48778766/xdocument-parsing-error
    public static class AdifDictionaryExtensions
    {
        public static Dictionary<decimal, string> ExtractAdifDictionary(TextReader reader)
        {
            Dictionary<decimal, string> dict = new Dictionary<decimal, string>();

            using (var xmlReader = XmlReader.Create(reader))
            {
                var xd = XDocument.Load(xmlReader);
                var ns1 = (XNamespace)"http://www.clublog.org/cty/v1.0";
                var ns2 = (XNamespace)"https://clublog.org/cty/v1.0";

                dict =
                    xd
                    .Root
                    .Elements("entities", ns1, ns2).Single()
                    .Elements("entity", ns1, ns2)
                    .ToDictionary(
                        x => (decimal)x.Elements("adif", ns1, ns2).Single(),
                        x => x.Elements("name", ns1, ns2).Single().Value);

                return dict;
            }
        }
    }

    public static class XContainerExtensions
    {
        public static IEnumerable<XElement> Elements(this XContainer container, string localName, XNamespace nameSpace, params XNamespace[] additionalNamespaces)
        {
            if (container == null || localName == null)
                throw new ArgumentNullException();
            var names = new[] { nameSpace }.Concat(additionalNamespaces).Select(ns => ns + localName).ToArray();
            return container.Elements().Where(e => names.Any(n => n == e.Name));
        }
    }

    class TestClass
    {
        public static void Test()
        {
			foreach (var xml in GetXml())
			{
				Console.WriteLine("\nInput XML: ");
				Console.WriteLine(xml);
	
				var dict = AdifDictionaryExtensions.ExtractAdifDictionary(new StringReader(xml));
	
				Console.WriteLine("Deserialized dictionary: ");
				Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented));
			}
        }

        static IEnumerable<string> GetXml()
        {
            var xml = @"<clublog date=""2018-02-13T21:30:11+00:00"" 
        xmlns=""https://clublog.org/cty/v1.0"">
<entities>
<entity>
    <adif>1</adif>
    <name>CANADA</name>
    <prefix>VE</prefix>
    <deleted>FALSE</deleted>
    <cqz>5</cqz>
    <cont>NA</cont>
    <long>-80.00</long>
    <lat>45.00</lat>
</entity>
<entity>
    <adif>2</adif>
    <name>ABU AIL IS</name>
    <prefix>A1</prefix>
    <deleted>TRUE</deleted>
    <cqz>21</cqz>
    <cont>AS</cont>
    <long>45.00</long>
    <lat>12.80</lat>
    <end>1991-03-30T23:59:59+00:00</end>
</entity>
<!--Additional entities omitted-->
</entities>
</clublog>
";
            return new[] { xml, xml.Replace(@"https://clublog.org/cty/v1.0", @"http://www.clublog.org/cty/v1.0")};
        }
    }

    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
						
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Uncaught exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;