Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET JAXB equivalent?

Tags:

c#

.net

jaxb

Is there an equivalent library for JAXB in .NET? I am trying to convert an XML I get to a .NET class. I have the XSD, but not sure how to convert the XML received into a concrete Class? I used the XSD tool to generate a class from the schema, but what I want to to convert the XML I receive on the fly to a object that I can work with in code.

I've seen the thread here that deals with this, but my query is - I want the object created to contain the data that I receive in the XML (i.e. the field values must be populated).

like image 324
Joseph Avatar asked Mar 13 '10 05:03

Joseph


1 Answers

You can use xsd.exe to generate the class and then use XmlSerializer in your code to populate the class. For example if xsd.exe creates a class called Foo you could write:

Foo someFoo;
using (var stream = new FileStream("foo.xml", FileMode.Open))
{
    var serializer = new XmlSerializer(typeof(Foo));
    someFoo = serializer.Deserialize(stream);
}
like image 58
Mike Two Avatar answered Nov 11 '22 07:11

Mike Two