Deserialize XML object returned from SQL query?

I need to deserialize an XML object that needs to be returned from a SQL query.

I was working in JSON, but can't use JSON, so I'm going to XML. The JsonConvert functionality gets my result in one line .. but I'm not really sure how to handle what SQL is giving me.

When writing to the server, the table gets the Xdocument type into an xml data 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

      

+3


source to share


3 answers


I am only adding this answer because you said you cannot use System.IO. If you can use System.IO see DatVM's answer. Here's an example of what you could do. I've simplified this so that it includes a class for deserialization and can be run in any console application.

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

      

+1


source


The XMLSerializer Deserialize

method has no overload that takes a string. You can use Stream

(instead of MemoryStream ):

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

      



Ps: your variable names are terrible. You should see the C # Convention.

+2


source


But before that you need to create a class with an attribute [Serializable]

:

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

      

Then use the standard deserializer:

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

      

0


source







All Articles