Reputation: 502
I am trying to de-serialize the following XML file.
<?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>
but when I try to de-serialize it using the following code and classes
[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;
}
below is the de-serialization code
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();
}
}
}
but it gives the error
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.
and when I change the the datatype of one of the variables in the mentioned classes it then gives the error of
"Input string was not in a correct format."
I can't figure out what am I doing wrong :(
EDIT: when I try to de-serialize the following file, it gives all the above error
<?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>
while when I try to de-serialize the following file it gives no error and works fine, both the files are of the same nodes with the same format except the explicit encoding definition.
Upvotes: 0
Views: 380
Reputation: 1647
The System.Data.SqlClient.SqlException
with the message "String or binary data would be truncated" means that the size of one of the columns in the table you are inserting to is too small.
For example, if the Title
column in the database has a size of 255 characters but the contents of the <title>
element in the XML document is more than 255 characters, SQL Server will raise this error.
Upvotes: 2
Reputation: 9335
For one, an XML file is required to have only one root.
Maybe, you should place <pagination>
and <itemList>
inside a common <root>
element.
Upvotes: 1