I am receiving XML strings over a socket, and would like to convert these to C# objects.
The messages are of the form:
<msg>
<id>1</id>
<action>stop</action>
</msg>
How can this be done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to use the
xsd.exetool which gets installed with the Windows SDK into a directory something similar to:And on 64-bit computers:
And on Windows 10 computers:
On the first run, you use
xsd.exeand you convert your sample XML into a XSD file (XML schema file):This gives you
yourfile.xsd, which in a second step, you can convert again usingxsd.exeinto a C# class:This should give you a file
yourfile.cswhich will contain a C# class that you can use to deserialize the XML file you’re getting – something like:Should work pretty well for most cases.
Update: the XML serializer will take any stream as its input – either a file or a memory stream will be fine:
or use a StringReader: