JMK
JMK

Reputation: 28059

Is there any addons for Visual Studio for serialising XML documents?

When working with JSON, I can use NuGet to add JSON.Net and this allows me to create a class containing the elements of my JSON object and serialise this to a JSON string. This seems to work better than using String.Format(""); to just manually create the JSON.

For XML, I currently use XDocument to create any XML documents I need to. This works but it is quite a manual process which gets messy as the document gets larger. It is also difficult to maintain when I need to go back later and add to the document.

I have a hunch that I find this difficult and messy because I'm doing it wrong. I am creating my XML documents using functional concepts but am working with an Object Oriented language. Surely what I need to do is create an class and implement properties and a constructor to create an object with the same properties as the XML document, then use some sort of framework to serialise this to a string?

So my question is how do I serialise a class to XML using Visual Studio 2010 and C#?

Cheers

Upvotes: 0

Views: 55

Answers (1)

Oded
Oded

Reputation: 498904

You can create a simply C# class with the needed properties and decorate it with the different Data* attributes from the System.Runtime.Serisalization namespace then use the DataContractSerializer to produce the XML.

See Using Data Contracts on MSDN and the example on the DataContractSerializer page.

Upvotes: 1

Related Questions