[英]deserialize an XML object returned from SQL query?
I need to deserialize an XML object that 'should' be returned from a SQL query. 我需要反序列化一个“应该”从SQL查询返回的XML对象。
I had this working in JSON, but cannot use JSON so I am moving over to XML. 我有这个在JSON中工作,但不能使用JSON所以我转移到XML。 The JsonConvert functionality gets my result in one line.. but I am not really sure how to handle what SQL gives me. JsonConvert功能将我的结果放在一行..但我不确定如何处理SQL给我的结果。
When writing to the server the Table is getting an Xdocument type, into a xml datatype cell. 当写入服务器时,Table将获取Xdocument类型到xml数据类型单元格中。
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
XMLSerializer 's Deserialize
method does not have any overload that take a string. XMLSerializer的Deserialize
方法没有任何带字符串的重载。 You can use Stream
(using MemoryStream ) instead: 您可以使用Stream
(使用MemoryStream )代替:
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
player_after_load = (Player)XML_serializer.Deserialize(ms);
}
Ps: your variable names are terrible. Ps:你的变量名很糟糕。 You should see a C# Coding Convention. 您应该看到C#编码约定。
I'm only adding this answer because you said you cannot use System.IO. 我只是添加了这个答案,因为你说你不能使用System.IO。 If you can use System.IO please refer to DatVM's answer. 如果您可以使用System.IO,请参阅DatVM的答案。 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
But before that you need to create class with [Serializable]
attribute: 但在此之前,您需要使用[Serializable]
属性创建类:
[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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.