Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 958 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert XML String to Object

#1
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?

Reply

#2
If you have the xsd of the xml message then you can generate c# classes using the .Net xsd.exe tool.

This .Net classes can then be used to generate the xml.
Reply

#3
You can use xsd.exe to create schema bound classes in .Net then XmlSerializer to Deserialize the string : [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply

#4
In addition to the other answers here you can naturally use the [XmlDocument][1] class, for XML DOM-like reading, or the [XmlReader][2], fast forward-only reader, to do it "by hand".


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
Try this method to Convert Xml to an object. It is made for exactly what you are doing:

protected T FromXml<T>(String xml)
{
T returnedXmlClass = default(T);

try
{
using (TextReader reader = new StringReader(xml))
{
try
{
returnedXmlClass =
(T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
catch (InvalidOperationException)
{
// String passed is not XML, simply return defaultXmlClass
}
}
}
catch (Exception ex)
{
}

return returnedXmlClass ;
}

Call it using this code:

YourStrongTypedEntity entity = FromXml<YourStrongTypedEntity>(YourMsgString);
Reply

#6
Simply Run Your Visual Studio 2013 as Administration ...
Copy the content of your Xml file..
Go to Visual Studio 2013 > Edit > Paste Special > Paste Xml as C# Classes
It will create your c# classes according to your Xml file content.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through