Reputation: 393
I need to deserialize an XML object that 'should' be returned from a SQL query.
I had this working in JSON, but cannot use JSON so I am moving over to XML. The JsonConvert functionality gets my result in one line.. but I am not really sure how to handle what SQL gives me.
When writing to the server the Table is getting an Xdocument type, into a xml datatype cell.
if (do_sql_read)
{
List<string> usernames = new List<string>();
List<int> ids = new List<int>();
string sql_load;
Player player_after_load = new Player();
//multiple
string select_string = @"SELECT * FROM [Table]";
using (SqlConnection sql_connection_a = new SqlConnection( GetConnectionString() ) )
{
sql_connection_a.Open();
using (SqlCommand command = new SqlCommand(select_string, sql_connection_a))
{
SqlDataReader reader = command.ExecuteReader(CommandBehavior.Default);
// XML VERSION
while (reader.Read())
{
int iii = reader.GetInt32(0); // unique id int
string name = reader.GetString(1); // Name string
sql_load = reader.GetString(2);
usernames.Add(name);
ids.Add(iii);
XmlSerializer XML_serializer = new XmlSerializer (typeof(Player));
// <<<<< THIS PART ??? >>>
player_after_load = (Player)XML_serializer.Deserialize (sql_load);
Console.WriteLine("SQLPlayer: " + iii + " " + player_after_load.name + " " + player_after_load.health + " " + player_after_load.mana);
}
/* JSON VERSION WORKS
while (reader.Read())
{
int iii = reader.GetInt32(0); // unique id int
string name = reader.GetString(1); // Name string
sql_load = reader.GetString(2);
usernames.Add(name);
ids.Add(iii);
player_after_load = JsonConvert.DeserializeObject<Player>(sql_load);
Console.WriteLine("SQLPlayer: " + iii + " " + player_after_load.name + " " + player_after_load.health + " " + player_after_load.mana);
}
*/
}
}
} // end do_sql_string
Upvotes: 4
Views: 3936
Reputation: 682
I'm only adding this answer because you said you cannot use System.IO. If you can use System.IO please refer to DatVM's answer. Here is a sample of what you could do. I simplified this so that it includes a class to deserialize to, and it can be run in any console app.
using System;
using System.Xml;
using System.Xml.Serialization;
public class Player
{
public string Name {get; set;}
}
public class Program
{
public static void Main()
{
var str = "<Player><Name>Bobby</Name></Player>";
var doc = new XmlDocument();
var XML_serializer = new XmlSerializer(typeof(Player));
doc.LoadXml(str);
Player player_after_load;
using (var nodeReader = new XmlNodeReader(doc))
{
player_after_load = (Player)XML_serializer.Deserialize(nodeReader);
}
Console.WriteLine(player_after_load.Name);
}
}
Console Results
Bobby
Upvotes: 1
Reputation: 20658
XMLSerializer's Deserialize
method does not have any overload that take a string. You can use Stream
(using MemoryStream) instead:
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
player_after_load = (Player)XML_serializer.Deserialize(ms);
}
P.s: your variable names are terrible. You should see a C# Coding Convention.
Upvotes: 2
Reputation: 33
But before that you need to create class with [Serializable]
attribute:
[Serializable]
public class MyObject
{
public int n1;
public int n2;
public String str;
}
Then use standart deserializer:
IFormatter formatter = new BinaryFormatter();
MyObject obj = (MyObject) formatter.Deserialize(your_xml_object);
Upvotes: 0