Can someone please help me, I have this xml snippet
<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
<DataBoxID>123</DataBoxID>
<DefaultSendToAddressCollection>
<EmailAddress>email@whereEver.com</EmailAddress>
</DefaultSendToAddressCollection>
</EmailConfiguration>
I want to create a corressponding c# class from this. Before you say - "Just use xsd.exe", the output from Xsd cannot be serialized and deserialized correct, because it generates the class using partial classes.
Please can you tell me how to create this class.... here is the approach I took, but it doesn't work.
public class EmailConfiguration
{
private string dataBoxID;
public string DataBoxID
{
get { return dataBoxID; }
set { dataBoxID = value; }
}
private DefaultSendToAddressCollectionClass defaultSendToAddressCollection;
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection
{
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
}
And here is the class declaration for the subclass
public class DefaultSendToAddressCollectionClass
{
private string[] emailAddress;
public string[] EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}
Did you use VS2008's XSD?
Here's the output I got:
Generates serializable output:
Bare minimum working... looks like you are only required to add one attribute.
XSD.EXE is the tool that produces classes specifically for the purpose of XML Serialization. If it produces partial classes, that's because they work for XML Serialization. That's not what your problem is.
Try using XSD.EXE and serializing / deserializing. If you get an exception again, then please catch it and then post the results of ex.ToString().
You have two possibilities.
Method 1. XSD tool
Suppose that you have your XML file in this location
C:\path\to\xml\file.xml
You can find it in
Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
Or if you have Windows 8 can just start typing Developer Command Prompt in Start screencd /D "C:\path\to\xml"
xsd file.xml
xsd /c file.xsd
And that's it! You have generated C# classes from xml file in
C:\path\to\xml\file.cs
Method 2 - Paste special
Required Visual Studio 2012+
Edit > Paste special > Paste XML As Classes
And that's it!
Usage
Usage is very simple with this helper class:
All you have to do now, is:
Here you have some Online
XML <--> JSON
Converters: ClickUsing .NET 3.5:
This class will serialize the way you want. I changed your custom collection to a List and used the XmlArrayItem attribute to specify how each email address would be serialized. There are many such attributes to help you fine tune the serialization process.