utorak, 25. siječnja 2011.

XML to Class Generator

1. Visual Studio 2008 -> Visual Studio Tools -> Visual Studio Command Prompt

2. Convert xml to xsd
D:\\test>xsd test.xml

3. Convert xsd to class
D:\\xsd.exe -c -l:c# -n:test test.xsd

4. Add new class to project.

root class hast to use [Serializable] atribute to deserialize

[Serializable]
[XmlRoot("response")]

public partial class TestKlasa


5. Deserialize

static TestKlasa Deserialize_Test(string response)
{
byte[] byteArray = Encoding.UTF8.GetBytes(response);
MemoryStream responseStream = new MemoryStream(byteArray);

try
{
XmlSerializer deserializer = new XmlSerializer(typeof(TestKlasa));
return (TestKlasa )deserializer.Deserialize(responseStream);
}
catch (Exception e)
{ ;}
finally
{
responseStream.Close();
}

return new TestKlasa();
}
}




Sources: here and here