建设部监理工程师网站,宿迁网站建设案例,网站建设的原则,中国企业报官网XML转换为实体类的错误处理方案 一.错误描述#xff1a; XML反序列化出错#xff0c;XML 文档(2, 2)中有错误二.解决方案#xff1a; 在实体类的字段要加上XmlElement属性三.具体实现: 1.XML文档 EVENT_INSTANCEEventTypeALTER_TABLE/EventType…XML转换为实体类的错误处理方案 一.错误描述 XML反序列化出错XML 文档(2, 2)中有错误二.解决方案 在实体类的字段要加上XmlElement属性三.具体实现: 1.XML文档 EVENT_INSTANCEEventTypeALTER_TABLE/EventTypePostTime2015-08-04T10:21:14.670/PostTimeSPID175/SPIDServerNameE6SER14/ServerNameLoginNamesa/LoginNameUserNamedbo/UserNameDatabaseNameE6ETms/DatabaseNameSchemaNameETms/SchemaNameObjectNameDriver/ObjectNameObjectTypeTABLE/ObjectTypeAlterTableActionListDropConstraintsNameDF_Driver_DriverID/Name/Constraints/Drop/AlterTableActionListTSQLCommandSetOptions ANSI_NULLSON ANSI_NULL_DEFAULTON ANSI_PADDINGON QUOTED_IDENTIFIERON ENCRYPTEDFALSE /CommandTextALTER TABLE ETms.DriverDROP 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 Liststring 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