Get name of image in Nopcommerce
Asked Answered
E

2

7

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();
Enlil answered 23/12, 2016 at 7:16 Comment(2)
Please add sample link you want and sample image name doyou want to findTakeshi
I think you are going to have to scrape the html <img> tags to get the name of the image.Oceanid
F
3

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();
}
Frey answered 31/12, 2016 at 19:26 Comment(0)
B
2

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

Badmouth answered 30/12, 2016 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.