0

I have a web service that hit by the client and returns an XML string of serialized XML object.

I have this class that serialized to XML:

public class ControlValveResultModel
{
    public string Selection { get; set; }
    public int PipeCount { get; set; }
    public int ControlValveCount { get; set; }
    public int ServiceValveCount { get; set; }
}

Here the function that makes serialization of the object before it sent to yhe client:

    public string ToXML<T>(T dataToSerialize)
    {
        try
        {
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stringwriter, dataToSerialize);
            return stringwriter.ToString();
        }
        catch
        {
            throw;
        }
    }

Here how looks object in the client:

"&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ControlValveResultModel xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;     &lt;Selection&gt;184028.884942584,664923.573252621,183875.392224311,664759.166484197&lt;/Selection&gt;
  &lt;PipeCount&gt;78&lt;/PipeCount&gt;
  &lt;ControlValveCount&gt;4&lt;/ControlValveCount&gt;
  &lt;ServiceValveCount&gt;26&lt;/ServiceValveCount&gt;
&lt;/ControlValveResultModel&gt;"

In client-side, I try to convert object above to this:

"<?xml version="1.0" encoding="utf-16"?>
<ControlValveResultModel xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Selection>184028.884942584,664923.573252621,183875.392224311,664759.166484197</Selection>
  <PipeCount>78</PipeCount>
  <ControlValveCount>4</ControlValveCount>
  <ServiceValveCount>26</ServiceValveCount>
</ControlValveResultModel>"

I tried this to decode:

var stringXML = decodeURIComponent(data)

But stringXML is not decoded.

How can I decode the XML above and get value?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • Does this answer your question? [Parse XML using JavaScript](https://stackoverflow.com/questions/17604071/parse-xml-using-javascript) – Patrick Beynio May 14 '20 at 00:18
  • Use following : System.Net.WebUtility.HtmlDecode(string). Serialize will not work wit utf-16 so you have to skip first line. So when deserializing I using put int a StringReader or StreamReader and the use ReadLine() before deserializing. – jdweng May 14 '20 at 00:57

1 Answers1

-1

Here is a solution:

var stringXML = data.replace(/&lt;/g, '<').replace(/&gt;/g, '>');

I wrote my own decoder.

Michael
  • 13,950
  • 57
  • 145
  • 288