我想用Nopcommerce网站上的产品列表创建XML。因为图像是二进制的,我无法找到一种方法只获取图像的名称(我只需要图像名称不在图像上显示)任何想法?我丢失的代码在/ 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();
答案 0 :(得分:3)
如果我没有错误,那么单个产品可能有多张图片,可能你必须使用循环。
由于您有产品ID,您可以使用图片服务获取图片搜索名称。
图片服务GetPicturesByProductId
为您提供了一个图片列表,您必须得到它的序列号
var pictures = _pictureService.GetPicturesByProductId(items.id)
foreach(item in pictures)
{
writer.WriteStartElement("IMAGE");
writer.WriteString(item.SeoFilename);
writer.WriteEndElement();
}
答案 1 :(得分:2)
Xml不支持二进制数据,您必须将其转换为文本格式,因为Xml用作按摩格式。你需要smth。像这样:
string text = System.Text.Encoding.UTF8.GetString(data);
以下是链接:binary-xml
byte[] binaryData;
try
{
binaryData = System.Convert.FromBase64String(base64String);
}
这是XmlReader的替代方案。请检查此链接:alternative
广告如果您坚持使用XmlReader尝试实现此目的:
writer.WriteStartElement("Image");
writer.WriteBase64(fileData[1], 0, fileData[1].Length);
我不确定这对你有帮助。没有经历这么多,但我很高兴,如果这将有任何帮助