1 Reply Latest reply on Aug 25, 2016 5:09 AM by Patrick Abernathy

    How to store REST API returned value into database in C#

    Shaw Qian

      Hi, I am new to C# and Tableau API. Recently I am doing a project which use Tableau REST API to Query Groups. Then store the returned values into database using C#.

       

      I am not quite sure what is the datatype of the Httpresponse function returned value, is that already a xml data string?

       

      Also anyone has experience  in retrieving the xml data and inserting into database?

        • 1. Re: How to store REST API returned value into database in C#
          Patrick Abernathy

          Good morning Shaw.  Tableau returns all their data via XML.  You have a couple of different options in storing the return data into a database.  Since the Group XML object isn't very large, it might be easiest to use XML Deserializer to import the XML into an object then have separate data columns in your database for each value.  Here is a quick example from StackOverflow: c# - How to deserialize xml to object - Stack Overflow.  If you don't really need to be able to query specific data columns and just want to store the data for later use in code, you could convert the XML into a JSON string and store the data that way.  I find that working with the JSON object in code is a lot easier than setting everything up to use the XML Deserializer but since JSON is just basically a big string, it may not work out the best for using it in a database.  I've pasted some code that I use to convert the XML to JSON below.  A third option is .Net does have a XML object but I find it a pain to work with but it is a third option if you want to check it out.  Hope this helps.

           

           

          public dynamic DeserializeXml(string xml)
          {
              if (string.IsNullOrEmpty(xml))
              {
                  return string.Empty;
              }
          
              var doc = new XmlDocument();
              doc.LoadXml(xml);
              var jsonText = JsonConvert.SerializeXmlNode(doc).Replace("@", "");
              return JsonConvert.DeserializeObject(jsonText);
          }