反序列化从 SQL 查询返回的 XML 对象?

deserialize an XML object returned from SQL query?

我需要反序列化从 SQL 查询返回 'should' 的 XML 对象。

我在 JSON 中使用此功能,但无法使用 JSON,所以我要转到 XML。 JsonConvert 功能在一行中得到了我的结果。但我不太确定如何处理 SQL 给我的结果。

写入服务器时,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. You can use Stream (using MemoryStream) 而不是:

using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
    player_after_load = (Player)XML_serializer.Deserialize(ms);
}

P.s: 你的变量名很糟糕。您应该看到 C# 编码约定。

但在此之前,您需要创建具有 [Serializable] 属性的 class:

[Serializable]
public class MyObject
{
     public int n1;
     public int n2;
     public String str;
 }

然后使用标准解串器:

IFormatter formatter = new BinaryFormatter();
MyObject obj = (MyObject) formatter.Deserialize(your_xml_object);

我添加这个答案只是因为你说你不能使用 System.IO。如果可以使用 System.IO 请参考 DatVM 的回答。这是您可以执行的操作的示例。我对此进行了简化,以便它包含一个要反序列化的 class,并且在任何控制台应用程序中它都可以是 运行。

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);

    }
}

控制台结果

Bobby