I want to create XML with list of products from Nopcommerce website. Because the image is in binary i cant find a way to get only the name of the image (i only need the image name not to display it on Image) any idea? my missing code is in the line of /images/thumbs/
topsystemDataClassesDataContext db = new topsystemDataClassesDataContext();
XmlTextWriter writer = new XmlTextWriter(path + "/Products_" + catid + ".xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("store");
//CREATE-SCREENS
var allProducts = (from p in db.Product_Category_Mappings
where p.CategoryId == catid
join s in db.Products
on p.ProductId equals s.Id
join im in db.Product_Picture_Mappings
on p.ProductId equals im.ProductId
join imag in db.Pictures
on im.PictureId equals imag.Id
select new
{
s.Name,
s.Id,
s.Price,
s.ShortDescription,
s.BackorderModeId,
im.PictureId,
imag.PictureBinary
}).ToList();
foreach (var item in allProducts)
{
writer.WriteStartElement("product");
writer.WriteStartElement("PRODUCT_URL");
writer.WriteString("http://www.topsystems.co.il/Product.aspx?ProductId=" + item.Id);
writer.WriteEndElement();
writer.WriteStartElement("product_name");
writer.WriteString(item.Name);
writer.WriteEndElement();
writer.WriteStartElement("MODEL");
writer.WriteString(item.BackorderModeId.ToString());
writer.WriteEndElement();
writer.WriteStartElement("CATALOG_NUMBER");
writer.WriteString("0");
writer.WriteEndElement();
writer.WriteStartElement("DETAILS");
writer.WriteString(item.ShortDescription);
writer.WriteEndElement();
writer.WriteStartElement("CURRENCY");
writer.WriteString("ILS");
writer.WriteEndElement();
writer.WriteStartElement("PRICE");
writer.WriteString(item.Price.ToString());
writer.WriteEndElement();
writer.WriteStartElement("SHIPMENT_COST");
writer.WriteString("0");
writer.WriteEndElement();
writer.WriteStartElement("DELIVERY_TIME");
writer.WriteString("3");
writer.WriteEndElement();
writer.WriteStartElement("MANUFACTURER");
writer.WriteString("ללא");
writer.WriteEndElement();
writer.WriteStartElement("WARRANTY");
writer.WriteString("ללא");
writer.WriteEndElement();
writer.WriteStartElement("IMAGE");
writer.WriteString("http://www.topsystems.co.il/content/images/thumbs/");
writer.WriteEndElement();
writer.WriteStartElement("TAX");
writer.WriteString("0");
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/xml";
Response.WriteFile(Server.MapPath("~/zap/Products_" + catid + ".xml"));
Response.Flush();
Response.End();
If I'm not wrong, then single product may have more than one pictures, probably you have to use loop for that same.
As you have product ids, you can use picture service to get picture seo name.
Picture service GetPicturesByProductId
gives you a list of pictures and you have to just get it's seoname
var pictures = _pictureService.GetPicturesByProductId(items.id)
foreach(item in pictures)
{
writer.WriteStartElement("IMAGE");
writer.WriteString(item.SeoFilename);
writer.WriteEndElement();
}
Xml does not support binary data, you have to convert it to a text format because Xml is used as a massage format. You need smth. like this:
string text = System.Text.Encoding.UTF8.GetString(data);
Here is the link: binary-xml
byte[] binaryData;
try
{
binaryData = System.Convert.FromBase64String(base64String);
}
This here is an alternative for XmlReader. Check this link: alternative
Ad if you are sticking with XmlReader try to implement this:
writer.WriteStartElement("Image");
writer.WriteBase64(fileData[1], 0, fileData[1].Length);
Also see this: stack-discussion
I'm not sure if this will help you. Not experienced with this so much but I'm glad if this will be of any help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With