0 Replies - 1050 Views - Last Post: 27 April 2021 - 08:46 AM

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 346
  • Joined: 25-May 16

Refactoring RestSharp XML

Posted 27 April 2021 - 08:46 AM

Hi everyone, I am trying to refactor some code due to a code scan not liking a library that I am using.

In method 1, I have a working example but I am trying to get it to use method 2 since it's a microsoft-supported library.
Does anyone know of a way where I can have the ContentType and RootElement and Namespace defined? When I create the xml, I have to send it using a REST call to another api that accepts the xml and the xml has to have that specific format.

// Method 1: using RestSharp
                RestSharp.Serializers.XmlSerializer serializer = new RestSharp.Serializers.XmlSerializer();
                serializer.ContentType = lookup.Where(dl => dl.Code == Constants.SetNameCode.ContentType).FirstOrDefault().Value;
                serializer.RootElement = lookup.Where(dl => dl.Code == Constants.SetNameCode.UpdateEnvelopeDataRootElement).FirstOrDefault().Value;
                serializer.Namespace = lookup.Where(dl => dl.Code == Constants.SetNameCode.Namespace).FirstOrDefault().Value;

                xml1 = serializer.Serialize(esignRequest.EsignRequest);

                // Method 2: Using System.Xml.Serialization - Trying to Refactor using this
                System.Xml.Serialization.XmlSerializer serializer2 = new System.Xml.Serialization.XmlSerializer(typeof(EsignRequest));
                using (var sww = new StringWriter())
                {
                    using (XmlWriter writer = XmlWriter.Create(sww))
                    {
                        serializer2.Serialize(writer, esignRequest.EsignRequest);
                        xml2 = sww.ToString();
                    }
                }


Any insight/help would be much appreciated

Is This A Good Question/Topic? 0
  • +

Page 1 of 1