2012-02-13 62 views
0

我正在尝试反序列化下面的XML文件。这个用于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> 

而当我尝试反序列化FOLL由于它没有提供错误并且工作正常,两个文件都是具有相同格式的相同节点,除了明确的编码定义。

+0

您是否确实需要为每个项目打开数据库连接?似乎你可以将它从你的foreach中移出(extImage中的资源链接块 – 2012-02-13 16:34:36

+0

然后它给了我这个错误变量名'@Asset_Item_id'已经被声明。变量名在查询批处理或存储过程中必须是唯一的' – 2012-02-13 16:47:19

+0

好吧,那是因为你已经添加了它们,你不能多次添加它们,将'.Parameters.Add()...'代码移动到块的外部,将它们添加到那里并引用它们通过它们在'foreach'块中的命名值。 – 2012-02-13 16:50:59

回答

2

消息“字符串或二进制数据将被截断”的System.Data.SqlClient.SqlException表示您插入的表中某列的大小太小。

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

+0

我有所有的数据库字段设置为nvarchar(50),它不够? – 2012-02-13 15:57:01

+0

可能没有,因为你得到这个错误。如果可能,请尝试更改为'nvarchar(MAX)'。 – dillenmeister 2012-02-13 15:59:54

1

是这条线吗?

{Requested Salesforce fields e.g:} 
+0

没有其可变数目字段(节点)的例子 – 2012-02-13 16:00:30

1

其中之一,XML文件只需要一个根。

也许,您应该将<pagination><itemList>放置在共同的<root>元素内。

+0

我把它们放在共同根目录searchResult – 2012-02-13 15:55:47

+0

是的,对不起。看到@dillenmeister的回答,它是正确的。 – linepogl 2012-02-13 15:57:17