1

All,

I am looking for XML mapping library for .net. I want to map an object to XML and later take XML and convert it back to the object. I know there's native support for this type of mapping in .net but is very limited and rigid. I am looking more for a library such as Castor (http://www.castor.org/) that exists in Java. If you have knowledge of any other XML mapping library that is flexible feel free to comment. Thanks.

ActiveX
  • 1,064
  • 1
  • 17
  • 37
  • 3
    Can you specify what actually didnt satisfy you in standard .net serialization? – Anton Semenov Apr 13 '11 at 16:55
  • 1. .net Serialization using reader/writer for this task is obviously tedious 2. When it comes the XML mapping, it is very rigid in terms that you can only specify very simplistic hiearchy and the rules for nesting collection elements are very limited and strictly dictate the object you define that maps to XML. – ActiveX Apr 13 '11 at 20:12
  • I still don't see what you think is so "very limited" about serialization. Maybe you could extend the question to specify precisely what problem you are trying to avoid or solve. – Cheeso Apr 13 '11 at 21:46
  • It is more difficult to explain without an example especially if you haven't used a flexible XML mapping framework. Best is to state few limitations: 1. Why would you ever want an object structure resemble XML structure (both should be independent)? 2. I believe with .net all serializable properties must be public! 3. .net 2.0 only supports arrays (in terms of collections) and basic object structure (it will throw exception on more complex object structures - easy to confuse the XML serializer). These are pobably the use/or not to use arguments. – ActiveX Apr 13 '11 at 22:04
  • I ended up still using it with shaping my object to the rules. I ended up with this goofy object structure with bunch of public arrays etc. If you look at the link above and take a look at Castor you will realize what a good mapping framework is. Here it is again: http://www.castor.org/ – ActiveX Apr 13 '11 at 22:05

4 Answers4

2

Xsd2Code is the best I have come across.

There is also an online tool, CodeXS which is pretty good. Microsoft also has XSDObjectGen which is better than the tool Visual Studio uses, but neither are as good as Xsd2Code.

Bob
  • 97,670
  • 29
  • 122
  • 130
  • This is more of a generator that generates classes from XML? I am looking for mapping an existing type to XML and let the run-time do the rest. – ActiveX Apr 13 '11 at 20:15
  • If you have existing classes and you can't serialize nicely to the XML format you want I would use the generated classes an implimnet the adapter pattern http://en.wikipedia.org/wiki/Adapter_pattern to map between the two. It sounds like your mapping is not going to be 1 to 1. In that case I can't think of an automated way to map between types. – Bob Apr 13 '11 at 21:02
  • Thanks for your response. That's a bit overkill. I have really single class that has elements that nest other elements which are 1 .. many (collection of elements) - stores my configuration of a hardware device (this file is really one of many different possible configurations user can load/interchange). I rather live with my goofy looking object that resembles the hierarchy of the XML I want at the end of the day for now but was hoping a more powerful library - I guess there's nothing out-there. – ActiveX Apr 13 '11 at 21:26
2

There are two kinds of XML and objects mapping. If you start from the .NET objects, serialize into XML, and turn it back into objects, that's called XML serialization. For that see Serialization and Deserialization.

On the other hand, if you know exactly what the XML should look like, you want to parse them automatically into objects, and turn them back into XML. You would first prepare a schema definition (in XSD or Relax NG) and generate a code. That's called XML data binding. See Comparison of xsd code generators.

Edit: I've never used this but I found a .NET port of XStream called xstream.net. I don't know if it compares to Castor but I liked XStream.

Community
  • 1
  • 1
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • I know this very well, and quite simply put it is very weak compared to what Java open source frameworks have to offer. Take a look at Castor, I wish I could find something that comes close to that. The task is simply to take existing type and have total freedom to map it to an XML hiearchy you want that doesn't have to resemble the object itself. That's the key, and to this effortless. – ActiveX Apr 13 '11 at 20:18
  • XStream - NICE FIND! That's more like it. I will take a look at it closer. – ActiveX Apr 13 '11 at 21:28
  • Bah, nice API but is using language features of .net version later than 2.0 - I would have to make it work with .net 2.0 which then would be out of sync with his version. – ActiveX Apr 13 '11 at 21:58
  • Update: I have converted it back to 2.0, simple modifications required, I will give it a run. – ActiveX Apr 13 '11 at 22:29
  • @ActiveX: Let us know how it goes! .NET serialization is severely limited, and the "must be public" bit alone has prevented me from using it. I resorted to writing my own serialization/deserialization logic based on reflection, but this was no generic congfigurable solution - just what I needed at the particular time and place. – The Dag Sep 13 '11 at 11:47
0

Will the following two methods solve your problem:

    public string GetXml(object obj, Type t)
    { 
        ///It will create XML using reflection
        ///but this xml wont have any attributes
        ///all properties will XMl elements
    }

    public object GetXml(string xml, Type t)
    {
        ///It will create the object from the XML
    }

I will make this methods as extended methods for type object. Then it will be lot more easier to access. Just let me know, I will implement this for you. This is my ID: sonalsavartkar@gmail.com

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
  • 1
    Ehm, "no". Yes, serialization and deserialization is involved. But the whole point concerns the ability to control the XML schema *independently* of the .Net types. There are many good reasons for wanting this. You could expose the same or a subset or superset of the information contained by them in many ways. You could map them to different schemas in order to query web services from different sources. The "traditional" .net approach to this appears to be writing mappers that create distinct types for every context, introducting bloat in code and at run-time... – The Dag Sep 13 '11 at 11:51
-1

We have created a framework which can auto-generate C# classes out of your XML. Its a visual item template to which you pass your XML and the classes are generated automatically in your project. Using these classes you can create/read/write your XML.

Check this link for the framework and Visual C# item template:click here

Savaratkar
  • 1,974
  • 1
  • 24
  • 44