Wiki

Clone wiki

XmlMapper / Home

XmlMapper

XmlMapper is an experimental .Net xml library. This library allow you to deserialize your xml into your class properties using XPath.

Go ahead and try:

$ hg clone https://slimmm@bitbucket.org/slimmm/xmlmapper

Download the latest version at http://nuget.org/packages/XmlMapper. Or install it using the Package Manager Console.

PM> Install-Package XmlMapper

Example

Let's assume you want to map this xml to your Party class.

<?xml version="1.0" encoding="utf-8" ?>
<party>
  <at street="100 the super street" country="France" city="Paris" />
  <people>
    <item id="123">
      <person firstName="jean" lastName="jack" age="20" />
    </item>
    <item id="456">
      <person firstName="pierre" lastName="paul" age="24" />
    </item>
  </people>
</party>

Here are your C# classes:

    [XmlMappable]
    public class Party
    {
        [XmlPath("//at")]
        public Address Address { get; set; }
        [XmlPath("//people/item/person")]
        public IEnumerable<Person> Persons { get; set; }
    }

    [XmlMappable]
    public class Address
    {
        [XmlPath("@street")]
        public string Street { get; set; }
        [XmlPath("@country")]
        public string Country { get; set; }
        [XmlPath("@city")]
        public string City { get; set; }
    }

    [XmlMappable]
    public class Person
    {
        [XmlPath("@firstName")]
        public string FirstName { get; set; }
        [XmlPath("@lastName")]
        public string LastName { get; set; }
        [XmlPath("@age")]
        public int Age { get; set; }
    }

Once the class properties are mapped, just deserialize your xml like this:

var deserializer = new XmlMapper.Deserializer();
//Parameter can be string or XElement
var p = deserializer.Deserialize<Party>(System.IO.File.ReadAllText(@"party.xml"));

To register namespaces, just add as many XmlNamespace attributes you need above your class:

    [XmlMappable]
    [XmlNamespace("theNs", "http://www.theNsSchema.com")]
    public class Party
    {
         [XmlPath("//theNs:people/theNs:item/theNs:person")]
         public IEnumerable<NsPerson> NsPersons { get; set; }
    }

To use your own custom property deserializer:

    [XmlMappable]
    [XmlNamespace("theNs", "http://www.theNsSchema.com")]
    public class Party
    {
        [XmlPath("//when/@date")]
        public int Year { get; set; }
        [XmlPath("//extra/extraItem")]
        public IEnumerable<ExtraItem> ExtraItems { get; set; }

        public int GetYear(XObject node)
        {
            return DateTime.Parse(((XAttribute) node).Value, new CultureInfo("en-US")).Year;
        }

        public IEnumerable<ExtraItem> GetExtraItems(IEnumerable<XObject> nodes)
        {
            foreach (XElement node in nodes)
            {
                var name = node.Attribute("name").Value;
                var id = node.Attribute("id").Value;
                yield return new ExtraItem { Id = name + id };
            }
        }
    }

GetYear method will be called to deserialize property Year and GetExtraItems to deserialize ExtraItems

Have fun!

Updated