Home > database >  Consume Web Service, Serialize XML response, save xml to database
Consume Web Service, Serialize XML response, save xml to database

Time:04-06

I have this situation, I am creating a Web Service in C# where I need to consume a SOAP Web Service, which gives me an XML response back, I need to serialize this XML response and save it to a table in the database.

I have tried the to call the XML in Postman and it worked fine with a

200 OK status

, but I need how to serialize this reponse and save it to the database.

And then I have tried to write this:

 public void CreateFilter(Student student)
        {
            var XML = XmlSerialization <Student> (student);

            ConnectDataBase db = new ConnectDataBase();

            SqlCommand cmd = new SqlCommand("sp_Student");
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = student.name;
            cmd.Parameters.Add("@surname", SqlDbType.VarChar).Value = student.surname;
            cmd.Parameters.Add("@subject", SqlDbType.VarChar).Value = student.subject;

            cmd.Parameters.Add("@student", SqlDbType.Xml).Value = XML;

        }

Any thoughts on how to get a response from the Web Service I have to consume, and then serialize the response I'm getting back and then save the serialization on the database?

Thank you in advance

CodePudding user response:

Well, you have a couple of options really. Essentially, if you aren't leveraging an ORM (like Hibernate or some such), you want to:

  1. Grab the XML payload
  2. Get that deserialized into an object instance so you can work with it
  3. Pick out the data you are interested in and persist it

Step 2 is essentially writing a class (or tree of classes) that mimmicks the field structure of the XML, then asking a library nicely to parse the XML state into an instance of that class. This then makes it easy to work with for you.

You can either leverage the the native deserialization as per:

https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.deserialize?view=net-6.0

Or, just for arguments sake alternatively you could use a modern serialization library. There isn't much of a good choice when it comes to XML and C#, so the main one I can think of that will get you from point a to point b is Json.NET:

https://www.newtonsoft.com/json

This library, though predomenantly pushed as an all-in-one object mapper for JSON is also able to translate between XML and Json:

https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm

Depending on how much of a fight the native deserializer puts up (defining the schema can be a bit of a pain, bloody SOAP am I right), it might be easier to use Json.NET to hoover up the XML, convert it to JSON, then deserialize that json as an object.

This gives you a 2-step deserialization process which isn't ideal, but it's not such a bad thing either as you get to work with the "nice" library and not have to fight the old baked in xml serialization stuff.

The choice is yours really. I'd give the first option a good go first then if that puts up too much of a fight you have Json.NET to fall back on :)

  • Related