- Home /
deserialize an XML object returned from SQL query?
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();
//single
string select_string = @"SELECT @seek_id FROM [Table]";
//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))
{
// single (remove if using the multiple results string)
command.Parameters.Add("@seek_id", SqlDbType.Int, 5).Value = tmp_id; // needed?
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));
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
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
Uhm, why can't you use JSON? JSON is a simpler and usually a more lightweight format. If the data don't need to be human readable it's quite common to have everything in one line (in both JSON and X$$anonymous$$L). That would be just unnecessary whitespace.
Besides that your query strings look a bit strange. I think this might be your actual problem... A select statement looks like this:
SELECT (column names) FRO$$anonymous$$ (tablename)
So between SELECT and FRO$$anonymous$$ you specify which columns from the table you want. If you don't add a WHERE clause at the end it will always return all rows. A "*" simply means all columns.
$$anonymous$$y guess is your "single" query should look like this:
"SELECT * FRO$$anonymous$$ [table] WHERE NameOfYourIDColumn=@seek_id"
NameOfYourIDColumn is the name of the column that should match your given value. Since we don't have your table layout we can't tell you that name ^^.
What kind of database is that? IÄm just curious since you direct connect to it. Is it a mysql, mssql, postgresql or a sqlite database? There are some $$anonymous$$or differences between those. Your table name also looks strange ^^
Targetting for Windows Phone and Unity.. Newtonsoft.json is not supported, System.IO is also not.
Complicates things for sure...
Well, i don't have a windows phone ^^. Ok using X$$anonymous$$L shouldn't be any different from the workflow. Have you checked what your query actually returned? Is it X$$anonymous$$L? Have you checked the db entry with external db management software to see if the data arrived there as it should?
At the moment you just say "It doesn't work" but we don't know what you have tried so far...
Also have you read my comment about your queries? It's also still not clear which database you're using.
Your answer
Follow this Question
Related Questions
Uploading custom filetype to MySQL database 1 Answer
Access a MySQL database via C# ? 2 Answers
Which is faster Update vs Delete Insert in Unity3d Database 1 Answer
[Quiz game] How do I make players contribute with questions? 1 Answer
Getting Specific mySql Row by searching table with a name 0 Answers