【发布时间】:2014-01-17 13:14:59
【问题描述】:
当我尝试反序列化来自 web 客户端的 rest 服务响应的 xml 时,出现以下错误。
{System.InvalidOperationException: XML 文档中存在错误 (1, 2)。 ---> System.InvalidOperationException: 不是 预期的。在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRoot.Read6_Root() --- 内部异常堆栈跟踪结束 --- 在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader、字符串 encodingStyle、对象事件)在 System.Xml.Serialization.XmlSerializer.Deserialize(流流)
在 XmlParser.MainPage.DeserializeXmlData(Stream 流) 在 XmlParser.MainPage.c_DisplayClass6.b_5(Object s, OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OpenReadOperationCompleted(Object arg)}
服务返回的xml是
<?xml version="1.0"?>
<ns0:Response xmlns:ns0="urn:ae:gov:testwebsite:uniqueness:genericcontentsrs:1">
<GenericContents>
<ModuleId>1296</ModuleId>
<Title>Getting around</Title>
<Description>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum., <a target="_blank" href="http://www.google.com">google.com</a>, provides useful information. People often rely on landmarks to give directions.<br /> <br />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. gmk’s Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</Description>
<BuildingId>0</BuildingId>
<GeoCoordinateX/>
<GeoCoordinateY/>
</GenericContents>
</ns0:Response>
我已经尝试为该类设置 XmlRoot 属性,但它仍然给出了相同的错误。
按照我用来反序列化 xml 的类
[XmlRoot(ElementName = "ns0:Response",)]
public class Root
{
[XmlElement(ElementName = "ns0:Response")]
public nsResponse _nsResponse { get; set; }
}
public class nsResponse
{
[XmlElement(ElementName = "GenericContents")]
public GenericContents _GenericContents { get; set; }
}
public class GenericContents
{
[XmlElement(ElementName = "ModuleId")]
public string _ModuleId { get; set; }
[XmlElement(ElementName = "Title")]
public string _Title { get; set; }
[XmlElement(ElementName = "Description")]
public string _Description { get; set; }
[XmlElement(ElementName = "BuildingId")]
public string _BuildingId { get; set; }
[XmlElement(ElementName = "GeoCoordinateX")]
public string _GeoCoordinateX { get; set; }
[XmlElement(ElementName = "GeoCoordinateY")]
public string _GeoCoordinateY { get; set; }
}
这是我用于反序列化的代码
private Root DeserializeXmlData(System.IO.Stream stream)
{
XmlSerializer ser = new XmlSerializer(typeof(Root));
Root result = (Root)ser.Deserialize(stream);
return result;
}
【问题讨论】:
-
向我们展示您用于反序列化该 xml 的代码
-
@Alberto 我已经添加了代码。请找到我的编辑。
标签: c# xml deserialization