
反序列化XML对象Symfony2问题,怎么解决
1个回答
展开全部
一.错误描述:
XML反序列化出错,XML 文档(2, 2)中有错误
在实体类的字段要加上XmlElement属性
- <EVENT_INSTANCE>
- <EventType>ALTER_TABLE</EventType>
- <PostTime>2015-08-04T10:21:14.670</PostTime>
- <SPID>175</SPID>
- <ServerName>E6SER14</ServerName>
- <LoginName>sa</LoginName>
- <UserName>dbo</UserName>
- <DatabaseName>E6ETms</DatabaseName>
- <SchemaName>ETms</SchemaName>
- <ObjectName>Driver</ObjectName>
- <ObjectType>TABLE</ObjectType>
- <AlterTableActionList>
- <Drop>
- <Constraints>
- <Name>DF_Driver_DriverID</Name>
- </Constraints>
- </Drop>
- </AlterTableActionList>
- <TSQLCommand>
- <SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" />
- <CommandText>ALTER TABLE ETms.Driver
- DROP CONSTRAINT DF_Driver_DriverID </CommandText>
- </TSQLCommand></EVENT_INSTANCE>
- [XmlRoot(ElementName = "EVENT_INSTANCE")]
- [Serializable]public class EventModel{
- [XmlElement(ElementName = "EventType")] public string EventType { set; get; }
- [XmlElement(ElementName = "PostTime")] public string PostTime { set; get; }
- [XmlElement(ElementName = "SPID")] public string Spid { set; get; }
- [XmlElement(ElementName = "ServerName")] public string ServerName { set; get; }
- [XmlElement(ElementName = "UserName")] public string UserName { set; get; }
- [XmlElement(ElementName = "DatabaseName")] public string DatabaseName { set; get; }
- [XmlElement(ElementName = "SchemaName")] public string SchemaName { set; get; }
- [XmlElement(ElementName = "ObjectName")] public string ObjectName { set; get; }
- [XmlElement(ElementName = "ObjectType")] public string ObjectType { set; get; }
- [XmlElement(ElementName = "TargetObjectName")] public string TargetObjectName { set; get; }
- [XmlElement(ElementName = "TargetObjectType")] public string TargetObjectType { set; get; }
- [XmlElement(ElementName = "PropertyName")] public string PropertyName { set; get; }
- [XmlElement(ElementName = "PropertyValue")] public string PropertyValue { set; get; }
- [XmlElement(ElementName = "Parameters")] public Parameters Parameters { get; set; }
- [XmlElement(ElementName = "TSQLCommand")] public TsqlCommand TsqlCommand { get; set; }
- }public class TsqlCommand{
- [XmlElement(ElementName = "CommandText")]public string CommandText { set; get; }
- [XmlElement(ElementName = "SetOptions")]public string SetOptions { set; get; }
- }public class Parameters{
- [XmlElement("Param")]public List<string> ParamContent { get; set; }
- }
- //xmlStr 是xml字符串 ;type是要转换目标实体的类型 【typeof (EventModel)】;public static object DeserializeFromXml(string xmlStr, Type type){ try
- { using (StringReader sr = new StringReader(xmlStr))
- {
- XmlSerializer xs = new XmlSerializer(type); return xs.Deserialize(sr);
- }
- } catch (Exception ex)
- { throw (ex);
- }
- }
二.解决方案:
三.具体实现:
1.XML文档
2.目标实体对象
3.XML转实体类测试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询