[英]Deserialize Xml C# with XmlSerializer
<response id=\"f57127c5-c7c5-4e31-bf60-a4b47ddb95c6\">
<error-code>0</error-code>
<error-message></error-message>
<result xsi:type=\"entityWrapper\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<elements>
<element xsi:type=\"decisionImpl\">
<cityId>0</cityId>
<createdDate>2015-08-21T14:58:46.570+07:00</createdDate>
<createdUser>5</createdUser
<effectiveDate>2015-07-05T17:03:44.947+07:00</effectiveDate>
<enterpriseId>5</enterpriseId>
<equipmentSystem>K</equipmentSystem>
<id xsi:type=\"xs:long\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">8</id><invoiceType>1;</invoiceType>
<modifiedDate>2015-08-21T14:58:47.045+07:00</modifiedDate>
<modifiedUser>5</modifiedUser><number>HC889</number>
<proponentName>Quang</proponentName>
<status>0</status>
</element>
</elements>
</result>
</response>
My Xml like that 我这样的Xml
public class Decision
{
[XmlAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public String type = Constants.Type.DECISION;
[XmlElement(ElementName = "id")]
public long id { get; set; }
[XmlElement(ElementName = "enterpriseId")]
public long enterpriseId { get; set; }
[XmlElement(ElementName = "number")]
public String number { get; set; }
[XmlElement(ElementName = "proponentName")]
public String proponentName { get; set; }
[XmlElement(ElementName = "equipmentSystem")]
public String equipmentSystem { get; set; }
[XmlElement(ElementName = "softwareApplication")]
public String softwareApplication { get; set; }
[XmlElement(ElementName = "processCreator")]
public String processCreator { get; set; }
[XmlElement(ElementName = "responsible")]
public String responsible { get; set; }
[XmlElement(ElementName = "effectiveDate")]
public DateTime effectiveDate { get; set; }
[XmlElement(ElementName = "cityId")]
public long cityId { get; set; }
[XmlElement(ElementName = "recipient")]
public String recipient { get; set; }
[XmlElement(ElementName = "invoiceType")]
public String invoiceType { get; set; }
[XmlElement(ElementName = "status")]
public int status { get; set; }
[XmlElement(ElementName = "createdDate")]
public DateTime createdDate { get; set; }
[XmlElement(ElementName = "modifiedDate")]
public DateTime modifiedDate { get; set; }
[XmlElement(ElementName = "createdUser")]
public String createdUser { get; set; }
[XmlElement(ElementName = "modifiedUser")]
public String modifiedUser { get; set; }
}
[Serializable]
[XmlRoot(ElementName = "response")]
public class MessageResponseWrapperList<T>
{
[XmlAttribute(AttributeName = "id")]
public String id { get; set; }
[XmlElement(ElementName = "error-code")]
public String errorCode { get; set; }
[XmlElement(ElementName = "error-message")]
public String errorMessage { get; set; }
[XmlElement(ElementName = "result")]
public DataWrapper<T> wrapper { get; set; }
}
[Serializable]
public class DataWrapper<T>
{
[XmlAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public String type = "entityWrapper";
[XmlArray(ElementName = "elements")]
[XmlArrayItem(ElementName = "element")]
public List<T> result { get; set; }
}
My function, I want to deserialize to MessageResponseWrapperList with List 我的功能,我想反序列化到带列表的MessageResponseWrapperList
public static MessageResponseWrapperList<T> fromXmlWrapper<T>(String xml)
{
StringReader reader = new StringReader(xml);
XmlSerializer serializer = new XmlSerializer(typeof(MessageResponseWrapperList<T>));
MessageResponseWrapperList<T> t = (MessageResponseWrapperList<T>)serializer.Deserialize(reader);
return t;
}
But I get error 但是我得到了错误
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll System.Xml.dll中发生了类型为'System.InvalidOperationException'的未处理异常
Additional information: There is an error in XML document (1, 165). 附加信息:XML文档(1,165)中有错误。
It's true that there's an error in XML document 确实,XML文档中存在错误
Change 更改
<createdUser>5</createdUser
to 至
<createdUser>5</createdUser>
Close bracket. 封闭支架。
A double quote doesn't need a backslash in an xml file. 双引号在xml文件中不需要反斜杠。 Below is your xml with all errors removed 以下是您的xml,其中删除了所有错误
<?xml version="1.0" encoding="utf-8" ?> <response id="f57127c5-c7c5-4e31-bf60-a4b47ddb95c6"> <error-code>0</error-code> <error-message></error-message> <result xsi:type="entityWrapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <elements> <element xsi:type="decisionImpl"> <cityId>0</cityId> <createdDate>2015-08-21T14:58:46.570+07:00</createdDate> <createdUser>5</createdUser> <effectiveDate>2015-07-05T17:03:44.947+07:00</effectiveDate> <enterpriseId>5</enterpriseId> <equipmentSystem>K</equipmentSystem> <id xsi:type="xs:long" xmlns:xs="http://www.w3.org/2001/XMLSchema">8</id> <invoiceType>1;</invoiceType> <modifiedDate>2015-08-21T14:58:47.045+07:00</modifiedDate> <modifiedUser>5</modifiedUser> <number>HC889</number> <proponentName>Quang</proponentName> <status>0</status> </element> </elements> </result> </response>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.