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);
    }
}

参考网址

转载于:https://www.cnblogs.com/wuyunblog/p/6625747.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值