我有一个由客户端命中并返回序列化XML对象的XML字符串的Web服务。
我有这个序列化为 XML 的类:
public class ControlValveResultModel
{
public string Selection { get; set; }
public int PipeCount { get; set; }
public int ControlValveCount { get; set; }
public int ServiceValveCount { get; set; }
}
这里是在将对象发送到客户端之前对对象进行序列化的函数:
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;
}
}
下面是客户端中对象的外观:
"<?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>"
在客户端,我尝试将上面的对象转换为:
"<?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>"
我尝试过解码:
var stringXML = decodeURIComponent(data)
但是字符串XML不会被解码。
如何解码上面的 XML 并获取值?
这是一个解决方案:
var stringXML = data.replace(/</g, '<').replace(/>/g, '>');
我编写了自己的解码器。