C# - JSON.NET-like library for XML?


  • And then the murders began.

    I can work with LINQ to XML or the other built-in stuff if I have to, but I'd rather not; the code is painful, especially for building XML. ;)

    Is there any library out there that can translate XML to/from POCOs, like JSON.NET does for JSON?


  • Notification Spam Recipient

    MSDN suggests this:

    Ha. Ha.

    This seems a little more like what you're looking for:


  • 🚽 Regular

    @Unperverted-Vixen said in C# - JSON.NET-like library for XML?:

    the code is painful, especially for building XML. ;)

    It's not that bad if in your POCO you only have public properties you want to export.

    public class Program
    {
    
        public static void Main()
        {
            var thing = new Thing(){
                Level = 42,
                Name = "A thing",
                Nested = new Nested(){
                    Where = "Right here",
                    When = DateTime.Today,
                },
                List = new List<bool> { true, false, false, true, true },
            };
            XDocument doc = Serialization.Serialize(thing);
            Console.WriteLine(doc.ToString());
        }
        
        public static class Serialization
        {
            public static T Deserialize<T>(XDocument doc)
            {
                var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
        
                using (var reader = doc.Root.CreateReader())
                {
                    return (T)xmlSerializer.Deserialize(reader);
                }
            }
        
            public static XDocument Serialize<T>(T value)
            {
                var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
        
                XDocument doc = new XDocument();
                using (var writer = doc.CreateWriter())
                {
                    xmlSerializer.Serialize(writer, value);
                }
                doc.Root.Attributes().Where(x => x.IsNamespaceDeclaration).Remove();  // So annoying
                return doc;
            }
        }
        
        public class Thing
        {
            public int Level {get;set;}
            public string Name {get;set;}
            public Nested Nested {get;set;}
            public List<bool> List {get;set;} = new List<bool>();
        }
        public class Nested
        {
            public string Where {get;set;}
            public DateTime When {get;set;}
        }
    }
    

    Result:

    <Thing>
      <Level>42</Level>
      <Name>A thing</Name>
      <Nested>
        <Where>Right here</Where>
        <When>2020-06-01T00:00:00+01:00</When>
      </Nested>
      <List>
        <boolean>true</boolean>
        <boolean>false</boolean>
        <boolean>false</boolean>
        <boolean>true</boolean>
        <boolean>true</boolean>
      </List>
    </Thing>

  • 🚽 Regular

    @Tsaukpaetra said in C# - JSON.NET-like library for XML?:

    This seems a little more like what you're looking for:

    It's doing pretty much what I posted above. 😆


  • And then the murders began.

    @Zecc said in C# - JSON.NET-like library for XML?:

    @Unperverted-Vixen said in C# - JSON.NET-like library for XML?:

    the code is painful, especially for building XML. ;)

    It's not that bad if in your POCO you only have public properties you want to export.

    I didn't think of XmlSerializer at all - I was only considering XmlDocument and LINQ to XML. That might be what I was looking for...


Log in to reply