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

參考網址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM