[英]Deserialize XML HTTP Response with C#
I am trying to write a serialization class, so that it would deserialize the http response from a camera device, however I and getting hung up on excluding the xsi:noNamespaceSchemaLocation tag. 我正在尝试编写一个序列化类,以便它可以反序列化来自摄像头设备的http响应,但是我会挂起来排除xsi:noNamespaceSchemaLocation标记。 The deserialization fails with "xsi" is an undeclared prefix error message.
反序列化失败,“xsi”是未声明的前缀错误消息。
XML Http Response: XML Http响应:
<root xsi:noNamespaceSchemaLocation='http://www.example.com/vapix/http_cgi/recording/stop1.xsd'><stop recordingid='20161125_121817_831B_ACCC8E627419' result='OK'/></root>
C# code: C#代码:
try
{
StopRecord ListOfStops = null;
XmlSerializer deserializer = new XmlSerializer(typeof(StopRecord));
using (XmlTextReader reader = new XmlTextReader(new StringReader(httpResponse)))
{
ListOfStops = deserializer.Deserialize(reader) as StopRecord ;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}
C# Serialization Class: C#序列化类:
public class StopRecord
{
[Serializable()]
[System.Xml.Serialization.XmlRoot("root")]
public class Root
{
public class stop
{
public stop(){}
[System.Xml.Serialization.XmlAttribute("recordingid")]
public string recordingid {get;set;}
[System.Xml.Serialization.XmlAttribute("result")]
public string result {get;set;}
}
}
}
Updated: Changed XmlElements to XmlAttributes. 更新:将XmlElements更改为XmlAttributes。 The problem with xsi still exists.
xsi的问题仍然存在。
The xsi:noNamspaceSchemaLocation attritbute shouldn't be deserialized as it is for the structure of the XML Document. xsi:noNamspaceSchemaLocation attritbute不应该像XML Document的结构那样反序列化。
Since recordingid and result are attributes, not elements, they need to be serialized as XmlAttribute instead of XmlElement. 由于recordingid和result是属性,而不是元素,因此需要将它们序列化为XmlAttribute而不是XmlElement。
public class StopRecord
{
[Serializable()]
[System.Xml.Serialization.XmlRoot("root")]
public class Root
{
public class stop
{
public stop(){}
[System.Xml.Serialization.XmlAttribute("recordingid")]
public string recordingid {get;set;}
[System.Xml.Serialization.XmlAttribute("result")]
public string result {get;set;}
}
}
}
Just wrap this xml response with a new root element where the xsi namespace is defined: 只需使用新的根元素包装此xml响应,其中定义了xsi名称空间:
<wrapper xmlns:xsi='http://www.example.com'>
<!-- original response goes here -->
<root xsi:noNamespaceSchemaLocation='http://www.example.com/vapix/http_cgi/recording/stop1.xsd'>
<stop recordingid='20161125_121817_831B_ACCC8E627419' result='OK'/>
</root>
</wrapper>
You also need to change your classes to add the Wrapper class - working example on .NET Fiddle : 您还需要更改类以添加Wrapper类 - .NET Fiddle上的工作示例 :
[Serializable]
[XmlRoot("wrapper")]
public class StopRecord
{
[XmlElement("root")]
public Root Root { get; set; }
}
public class Root
{
[XmlElement("stop")]
public Stop stop { get; set; }
}
public class Stop
{
[XmlAttribute("recordingid")]
public string recordingid { get; set; }
[XmlAttribute("result")]
public string result { get; set; }
}
There is no need to deserialize noNamspaceSchemaLocation attritbute. 无需反序列化noNamspaceSchemaLocation attritbute。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.