这个XML序列化程序有什么问题?

时间:2012-02-13 15:40:38

标签: c# xml serialization

我正在尝试反序列化以下XML文件。

<?xml version="1.0" encoding="UTF-8"?>

<searchResult>

<pagination>
    <itemsPerPage>{Number of Inventories per Page}</itemsPerPage>
    <numberOfItems>{Number of Inventories}</numberOfItems>
</pagination>

<itemList>
    <item>
        {Requested Salesforce fields e.g:}
        <Id>{Salesforce Id}</Id>
        <Name>{Name}</Name>
        <pb__IsForSale__c>{e.g.}false</pb__IsForSale__c>
        <pb__IsForLease__c>{e.g.}true</pb__IsForLease__c>
        <pb__ItemDescription__c>{Item Description}</pb__ItemDescription__c>
        <pb__PurchaseListPrice__c>{Item List Price e.g.:}2000000.00</pb__PurchaseListPrice__c>
        <CurrencyIsoCode>{Currency Iso Code e.g:}EUR</CurrencyIsoCode>
        <pb__UnitBedrooms__c>{Number of Bedrooms}</pb__UnitBedrooms__c>

        <asset>
            <Id>{internal Propertybase InventoryAsset Id}</Id>
            <category>{Images, Videos or Documents}</category>
            <isExternalLink>false</isExternalLink>
            <title>{title}</title>
            <filename>{original name of the uploaded file}</filename>
            <url>{full url to image/video/document}</url>
            <thumbnailUrl>{full url to thumbnail image}</thumbnailUrl>
            <midresUrl>{full url to thumbnail image}</midresUrl>
            <tags>{comma separated tags}</tags>
            <mimeType>{e.g. image/jpeg}</mimeType>
        </asset>

        <asset>
            <Id>{internal Propertybase InventoryAsset Id}</Id>
            <category>{Images, Videos or Documents}</category>
            <isExternalLink>true</isExternalLink>
            <title>{title}</title>
            <url>{full url to image/video/document}</url>
            <tags>{comma separated tags}</tags>
        </asset>

        <asset>
           {...}
        </asset>
        {more assets ...}

    </item>

    <item>
        {...}
    </item>
    {more items ...}

</itemList>

但是当我尝试使用以下代码和类对其进行反序列化时

[XmlRoot("searchResult")]
public class searchResult
{
    public page pagination;
    public item[] itemList;
}

public class page
{
    public string itemsPerPage;
    public string numberOfItems;
}
public class item
{
    public string Id;
    public string Name;
    public string pb__IsForSale__c;
    public string pb__IsForLease__c;
    public string pb__ItemDescription__c;
    public string pb__PurchaseListPrice__c;
    public string CurrencyIsoCode;
    public string pb__UnitBedrooms__c;
    public string pb__TotalAreaSqft__c;
    public string pb__UnitType__c;
    [XmlElement("asset")]
    public asset[] externalDocs;
}
public class asset
{
    public string id;
    public string category;
    public string isExternalLink;
    public string title;
    public string filename;
    public string url;
    public string thumbnailUrl;
    public string midresUrl;
    public string tags;
    public string mimeType;
}
下面的

是反序列化代码

    public void ReadPO(string filename)
{
    // Create an instance of the XmlSerializer class;
    // specify the type of object to be deserialized.
    XmlSerializer serializer = new XmlSerializer(typeof(searchResult));

    /* If the XML document has been altered with unknown 
    nodes or attributes, handle them with the 
    UnknownNode and UnknownAttribute events.*/
    serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
    serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);

    // A FileStream is needed to read the XML document.
    FileStream fs = new FileStream(filename, FileMode.Open);

    // Declare an object variable of the type to be deserialized.
    searchResult po;

    /* Use the Deserialize method to restore the object's state with
    data from the XML document. */
    po = (searchResult)serializer.Deserialize(fs);

    page dppage = po.pagination;
    item[] dpitems = po.itemList;
    foreach (item itemProp in dpitems)
    {
        asset[] extImage = itemProp.externalDocs;
        foreach (asset link in extImage)
        {
            SqlConnection conninsert = new SqlConnection();
            conninsert.ConnectionString = "Data Source=MAMOOR-5E14351F; Database=elysian_RealEstateDB; User Id=sa; Password=bhomes";
            SqlCommand cmdinsert = new SqlCommand("Insert Into testtable2(column1, column2, column3, column4, column5, column6, column7, column8, column9 ,column10) Values (@Id, @Category, @isExt, @Title, @FilName, @Url, @Thumb, @MidRes, @Tag, @mimeType)", conninsert);
            cmdinsert.Parameters.Add(new SqlParameter((@"Id"), SqlDbType.NVarChar) { Value = link.id });
            cmdinsert.Parameters.Add(new SqlParameter((@"Category"), SqlDbType.NVarChar) { Value = link.category });
            cmdinsert.Parameters.Add(new SqlParameter((@"isExt"), SqlDbType.NVarChar) { Value = link.isExternalLink });
            cmdinsert.Parameters.Add(new SqlParameter((@"Title"), SqlDbType.NVarChar) { Value = link.title });
            cmdinsert.Parameters.Add(new SqlParameter((@"FilName"), SqlDbType.NVarChar) { Value = link.filename });
            cmdinsert.Parameters.Add(new SqlParameter((@"Url"), SqlDbType.NVarChar) { Value = link.url });
            cmdinsert.Parameters.Add(new SqlParameter((@"Thumb"), SqlDbType.NVarChar) { Value = link.thumbnailUrl });
            cmdinsert.Parameters.Add(new SqlParameter((@"MidRes"), SqlDbType.NVarChar) { Value = link.midresUrl });
            cmdinsert.Parameters.Add(new SqlParameter((@"Tag"), SqlDbType.NVarChar) { Value = link.tags });
            cmdinsert.Parameters.Add(new SqlParameter((@"mimeType"), SqlDbType.NVarChar) { Value = link.mimeType });
            conninsert.Open();
            cmdinsert.ExecuteNonQuery();
            conninsert.Close();
            conninsert.Dispose();
        }
    }
}

但是它给出了错误

tring or binary data would be truncated.
The statement has been terminated.

Description: An unhandled exception occurred during
the execution of the current web request.
Please review the stack trace for more information
about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException:
String or binary data would be truncated.
The statement has been terminated.

当我更改上述类中某个变量的数据类型时,它会给出错误

"Input string was not in a correct format."

我无法弄清楚我做错了什么:(

编辑:当我尝试反序列化以下文件时,它会给出以上所有错误

<?xml version="1.0"?>
<searchResult>
  <pagination>
    <itemsPerPage>100</itemsPerPage>
    <numberOfItems>510</numberOfItems>
  </pagination>
  <itemList>
    <item>
      <Id>askldfasdklf</Id>
      <Name>asdfasdfa</Name>
      <pb__IsForSale__c>true</pb__IsForSale__c>
      <pb__IsForLease__c>false</pb__IsForLease__c>
      <pb__ItemDescription__c>asdfasdfasdf</pb__ItemDescription__c>
      <pb__PurchaseListPrice__c>220000.00</pb__PurchaseListPrice__c>
      <CurrencyIsoCode>UAE Dirham</CurrencyIsoCode>
      <pb__UnitBedrooms__c>asdfasdf</pb__UnitBedrooms__c>
      <pb__TotalAreaSqft__c>513.00</pb__TotalAreaSqft__c>
      <pb__UnitType__c>Apartment</pb__UnitType__c>
      <asset>
        <id>asdfasdfa</id>
        <category>asdfasdf</category>
        <isExternalLink>false</isExternalLink>
        <title>external video</title>
        <filename>1.jpg</filename>
        <url>asdfasdfadf</url>
        <thumbnailUrl>asdfasdfasdf</thumbnailUrl>
        <midresUrl>asdfasdfa</midresUrl>
        <tags>
        </tags>
        <mimeType>image/jpeg</mimeType>
      </asset>
    </item>
  </itemList>
</searchResult>

当我尝试反序列化以下文件时,它没有错误并且工作正常,两个文件都是相同的节点,除了显式编码定义之外具有相同的格式。

3 个答案:

答案 0 :(得分:2)

带有消息&#34;字符串或二进制数据的System.Data.SqlClient.SqlException将被截断&#34;表示要插入的表中某列的大小太小。

例如,如果数据库中的Title列的大小为255个字符,但XML文档中<title>元素的内容超过255个字符,则SQL Server将引发此错误

答案 1 :(得分:1)

这是这条线吗?

 {Requested Salesforce fields e.g:}

答案 2 :(得分:1)

首先,XML文件只需要一个根。

也许,您应该将<pagination><itemList>置于公共<root>元素中。