Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

            //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
like image 346
Michael Meritt Avatar asked Sep 19 '25 04:09

Michael Meritt


1 Answers

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.

like image 100
Luke Vo Avatar answered Sep 21 '25 18:09

Luke Vo