Tag Archives: xml

Convert JSON to XML in .NET

I was working on a project where we were receiving data in a JSON-like format, and we needed the ability to extract values by field name. After doing just a bit of research, I found that the .NET Framework has a JsonReaderWriterFactory class that allows you to create an XmlDictionaryReader that processes JSON data.

Converting JSON to XML is perfect for my needs because it allows me to use LINQ to XML to query and retrieve the values I’m looking for. So, I wrote a few regular expressions to transform the JSON-like format into actual JSON. Then I used the JsonReaderWriterFactory to do the conversion and loaded the resulting XML into an XDocument.

Here’s an example.

const string json = @"{
    ""foo"":""bar"",
    ""complexFoo"": {
        ""subFoo"":""subBar""
    }
}";
byte[] bytes = Encoding.ASCII.GetBytes(json);
using (var stream = new MemoryStream(bytes))
{
    var quotas = new XmlDictionaryReaderQuotas();
    var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas);
    var xml = XDocument.Load(jsonReader);
    Console.WriteLine(xml);
}

/*--------
 Output:
 <root type="object">
   <foo type="string">bar</foo>
   <complexFoo type="object">
     <subFoo type="string">subBar</subFoo>
   </complexFoo>
 </root>
--------*/
Advertisements

XSL Transformations in C#

In Visual Studio 2005 (yea–this happened a few years ago), Microsoft redesigned the architecture of their XSL transformations. It wasn’t difficult to execute a simple transformation before, but it’s downright ridiculous now.

Here’s a complete example of executing a simple XSL transformation:

using System.Xml.Xsl;

namespace Xml.XslTransformation
{
    class Program
    {
        static void Main(string[] args)
        {
            var prog = new Program();
            prog.Run();
        }

        public void Run()
        {
            var xslt = new XslCompiledTransform();
            xslt.Load("mapping.xslt");
            xslt.Transform("sample.xml", "output.xml");
        }
    }
}

XmlSerializer and Namespaces

Need to share some data with another system/program/thing? XML files are a great, easy way to do it! Typically, my process for creating or consuming XML files will be to generate a class from an XML schema definition (XSD) and then serialize to and deserialize from XML as needed.

Usually, you don’t need to do anything above and beyond this, but from time to time you might need to do some additional manipulation with the XML namespaces and associated prefixes. The XmlSerializer class gives you a lot of flexibility to do this, and it’s really easy! Here are some different things you can do along with the output produced.

Here’s my no-frills Person class that I’ll be serializing to XML in the examples that follow:

[XmlType(Namespace = "http://adamprescott/sample")]
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DateOfBirth { get; set; }
}

By default, the serializer will include the XML namespace on each element.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person));
serializer.Serialize(Console.Out, p);
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName xmlns="http://adamprescott/sample">James Q.</FirstName>
  <LastName xmlns="http://adamprescott/sample">Sample</LastName>
  <DateOfBirth xmlns="http://adamprescott/sample">1970-01-01T00:00:00</DateOfBirth>
</Person>

Whoa-day! That’s pretty busy. You can clean it up a lot just by declaring the default namespace when you instantiate your XmlSerializer.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person), "http://adamprescott/sample");
serializer.Serialize(Console.Out, p);
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://adamprescott/sample">
  <FirstName>James Q.</FirstName>
  <LastName>Sample</LastName>
  <DateOfBirth>1970-01-01T00:00:00</DateOfBirth>
</Person>

That’s a little nicer, but what if I don’t want those XMLSchema namespaces? That’s easy, too!

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person), "http://adamprescott/sample");
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, "http://adamprescott/sample");
serializer.Serialize(Console.Out, p, namespaces);
<?xml version="1.0"?>
<Person xmlns="http://adamprescott/sample">
  <FirstName>James Q.</FirstName>
  <LastName>Sample</LastName>
  <DateOfBirth>1970-01-01T00:00:00</DateOfBirth>
</Person>

Note that I made the prefixes all string.Empty. If I were to specify a prefix, it would be included in the serialized XML.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person));
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("ap", "http://adamprescott/sample");
serializer.Serialize(Console.Out, p, namespaces);
<?xml version="1.0"?>
<Person xmlns:ap="http://adamprescott/sample">
  <ap:FirstName>James Q.</ap:FirstName>
  <ap:LastName>Sample</ap:LastName>
  <ap:DateOfBirth>1970-01-01T00:00:00</ap:DateOfBirth>
</Person>