XML反序列化出错,XML 文档(2, 2)中有错误

XML转换为实体类的错误处理方案

一.错误描述:

  • XML反序列化出错,XML 文档(2, 2)中有错误

二.解决方案:

  • 在实体类的字段要加上XmlElement属性

三.具体实现:

1.XML文档
	<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>
2.目标实体对象
[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; }
}
3.XML转实体类测试
//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);
    }
}

参考网址

posted @ 2017-03-27 11:40  山中自有人  阅读(6637)  评论(0编辑  收藏  举报