public void LoadXML(LoadProductXMLFormViewModel loadProductXMLFormViewModel) { // associa o documento xml com o produto string xmlName = loadProductXMLFormViewModel.XmlName; string path = "C:\\Users\\Ricardo\\Documents\\TechnicalAdvisor\\TechnicalAdvisor\\XMLFiles\\"; string extensionFile = ".xml"; string fullXMLPath = path + xmlName + extensionFile; XmlProduct xmlProduct = new XmlProduct(); xmlProduct.ProductId = loadProductXMLFormViewModel.ID; xmlProduct.FileName = fullXMLPath; //try { // var getId = _context.XmlProduct.Last(i => i.Id != 0); // xmlProduct.Id = getId.Id++; //} //catch //{ // xmlProduct.Id = 1; //} _xMLService.SaveThis(xmlProduct); // salva no DB o objeto que associa o documento xml com o produto // já eras! }
public async Task GrabAsync(string baseAddress, List <Models.Reference> dataSet, XmlProduct selectors, string identifier = "<SKU>") { _grabbedData = new List <XmlProduct>(); var config = Configuration.Default.WithDefaultLoader(); foreach (var reference in dataSet) { var product = new XmlProduct(); var single = ""; if (identifier == "<SKU>") { single = reference.Sku.Replace(" ", "").Remove(0, 1); } else { single = reference.ProductId; } var document = await BrowsingContext.New(config).OpenAsync(baseAddress.Replace(identifier, single)); if (document.QuerySelector("body.product_ProductNotAvailable") != null) { continue; } if (document.QuerySelector("body.product_OfflineProduct") != null) { continue; } product.Name = document.QuerySelector(selectors.Name) != null?document.QuerySelector(selectors.Name).TextContent.Replace("\n", "") : null; if (product.Name == null) { continue; } product.Sku = reference.Sku; product.Gtin = reference.Gtin; product.Description = document.QuerySelector(selectors.Description) != null?document.QuerySelector(selectors.Description).TextContent.Replace("\n", "") : null; product.Picture = document.QuerySelector(selectors.Picture) != null?document.QuerySelector(selectors.Picture).GetAttribute("src").Replace("\n", "") : null; product.Categories = document.QuerySelectorAll(selectors.Categories.First()).ToList().Select(d => d.TextContent.Replace("\n", "")).ToArray(); product.Attributes = document.QuerySelectorAll(selectors.Attributes.First()) != null?document.QuerySelectorAll(selectors.Attributes.First()).ToList().Select(a => a.TextContent.Replace("\n", "")).ToArray() : null; product.Documents = document.QuerySelectorAll(selectors.Documents.First()) != null?document.QuerySelectorAll(selectors.Documents.First()).ToList().Select(d => d.GetAttribute("href")).ToArray() : null; _grabbedData.Add(product); } }
public XmlElement ProductNode(XmlProduct feedProduct) { var product = _document.CreateElement("product"); #region Product Attributes product.SetAttribute("sku", feedProduct.Sku); product.SetAttribute("gtin", feedProduct.Gtin); product.SetAttribute("fullName", feedProduct.Name); product.SetAttribute("type", "Stock"); product.SetAttribute("draft", "false"); #endregion // Descriptions if (!String.IsNullOrWhiteSpace(feedProduct.Description)) { // Short Description product.AppendChild(ProductCDataSection("shortDescription", feedProduct.Description)); // Full Description product.AppendChild(ProductCDataSection("fullDescription", feedProduct.Description)); } // Categories if (feedProduct.Categories != null && feedProduct.Categories.Skip(1).Take(2).Count() > 0) { product.AppendChild(ProductCategoriesNode(feedProduct.Categories.Skip(1).Take(2).ToArray())); } // Pictures if (!String.IsNullOrEmpty(feedProduct.Picture)) { product.AppendChild(ProductPictureNode(feedProduct)); } // Attributes if (feedProduct.Attributes != null && feedProduct.Attributes.Count() > 0) { product.AppendChild(ProductAttributesNode(feedProduct.Attributes)); } // Documents if (feedProduct.Documents != null && feedProduct.Documents.Where(d => d.EndsWith(".pdf")).Count() > 0) { product.AppendChild(ProductDocumentsNode(feedProduct.Documents.Where(d => d.EndsWith(".pdf")).ToArray())); } return(product); }
public XmlElement ProductPictureNode(XmlProduct feedProduct) { var node = _document.CreateElement("pictures"); var pictureNode = _document.CreateElement("picture"); pictureNode.SetAttribute("name", feedProduct.Sku); pictureNode.SetAttribute("type", feedProduct.Picture.Substring(feedProduct.Picture.LastIndexOf('.') + 1)); pictureNode.SetAttribute("format", "enum"); pictureNode.SetAttribute("url", feedProduct.Picture); pictureNode.SetAttribute("useOriginal", "false"); node.AppendChild(pictureNode); return(node); }
private async void Grab_Data_Commence(object sender, EventArgs e) { var selectors = new XmlProduct(); if (grabDataNameLabel.Checked && !string.IsNullOrWhiteSpace(grabDataNameTextBox.Text)) { selectors.Name = grabDataNameTextBox.Text; } if (grabDataFullDescriptionLabel.Checked && !string.IsNullOrWhiteSpace(grabDataFullDescriptionTextBox.Text)) { selectors.Description = grabDataFullDescriptionTextBox.Text; } if (grabDataPictureLabel.Checked && !string.IsNullOrWhiteSpace(grabDataPictureTextBox.Text)) { selectors.Picture = grabDataPictureTextBox.Text; } if (grabDataAttributesLabel.Checked && !string.IsNullOrWhiteSpace(grabDataAttributesTextBox.Text)) { selectors.Attributes = new string[] { grabDataAttributesTextBox.Text } } ; if (grabDataCategoryLabel.Checked && !string.IsNullOrWhiteSpace(grabDataCategoryTextBox.Text)) { selectors.Categories = new string[] { grabDataCategoryTextBox.Text } } ; if (grabDataDocumentsLabel.Checked && !string.IsNullOrWhiteSpace(grabDataDocumentsTextBox.Text)) { selectors.Documents = new string[] { grabDataDocumentsTextBox.Text } } ; WriteLine(consoleTextBox, "Grabbing data"); GrabController gc = new GrabController(); await gc.GrabAsync(grabSiteTextBox.Text, _comparisonData.ToList(), selectors); _grabbedData = gc._grabbedData; WriteLine(consoleTextBox, "Product(s) imported successfully: " + _grabbedData.Count); }
/// <summary> /// Parses the product for Xml Documents (multiple XML documents can be in a single product). /// </summary> /// <param name="product">The product.</param> /// <returns>IEnumerable<XmlProduct>.</returns> public static IEnumerable <XmlProduct> GetXmlProducts(this TextProduct product) { var startAt = 0; var seq = 1; var body = product.Content.RawBody; do { startAt = body.IndexOf(XmlHeader, startAt, StringComparison.Ordinal); if (startAt == -1) { yield break; } var endAt = body.IndexOf(XmlHeader, startAt + XmlHeader.Length, StringComparison.Ordinal); if (endAt == -1) { endAt = body.Length; } var xml = body.Substring(startAt, endAt - startAt).TrimEnd(); var newFilename = string.Concat( Path.GetFileNameWithoutExtension(product.Filename), '-', seq.ToString("00"), ".XML"); yield return(XmlProduct.Create( newFilename, product.TimeStamp, new TextContent { RawHeader = product.Content.RawHeader, RawBody = xml }, product.ReceivedAt, seq++, product.Source)); startAt = endAt; } while (startAt < body.Length); }
public void SaveThis(XmlProduct xmlproduct) { _context.Add(xmlproduct); _context.SaveChanges(); }
public void LoadJson(XmlProduct xmlProduct) { // recebe um produtoxml e abre o arquivo xml XElement root = XElement.Load(xmlProduct.FileName); var queryXML1 = from a in root.Element("Sections").Elements("ManualSection").Elements("Chapters").Elements("ManualChapter"). Elements("Paragraph").Elements("ManualParagraph") select a; //ele entra nos nós dos paragrafos, onde ele pega todas as infos necessarias para criar o objeto manualparagraph List <ManualParagraph> paragraphs = new List <ManualParagraph>(); foreach (var node in queryXML1) { ManualParagraph paragraph = new ManualParagraph(); paragraph.SectionTitle = node.Element("SectionTitle").Value; paragraph.ChapterTitle = node.Element("ChapterTitle").Value; paragraph.Texts = node.Element("Text").Value; paragraphs.Add(paragraph); } //agora ele vai montar as listas de seções, capítulos e de parágrafos!!! //montando a lista de capítulos: var queryXML2 = from a in root.Element("Sections").Elements("ManualSection").Elements("Chapters").Elements("ManualChapter") select a; List <ManualChapter> chapters = new List <ManualChapter>(); foreach (var node2 in queryXML2) { ManualChapter chapter = new ManualChapter(); chapter.Title = node2.Element("ChapterTitle").Value; chapters.Add(chapter); } //montando a lista de seções: var queryXML3 = from a in root.Element("Sections").Elements("ManualSection") select a; List <ManualSection> sections = new List <ManualSection>(); foreach (var node3 in queryXML3) { ManualSection section = new ManualSection(); section.Title = node3.Element("SectionTitle").Value; sections.Add(section); } //agora termina e continua o restante do código... // //entrando na parte do código que vai começar a se direcionar pra paginação // conta o numero total de paginas do manual int totalLines = 0; //Número inicial de páginas do manual foreach (var para in paragraphs) { var p = para.Texts.Count(); totalLines += p; } //agora é necessário dividir o numero total de paginas pelo numero total aceitável por página, que é arbitrário. //só fazendo testes pra ver mesmo, eu vou colocar um que seja conveniente nesse momento. int totalLinesOfAPage = 1000; // numero de linhas maximo de uma pagina! //cria listas que vao compor as paginas e que farão parte da instanciação do objeto "publicationProductViewModel" var pages = CreatePages(paragraphs, totalLinesOfAPage); Manual manual = new Manual("Carro loucaço>", pages, chapters, sections); manual.TotalPages = manual.Paragraphs.FirstOrDefault(p => p != null).TotalPages; var json = JsonConvert.SerializeObject(manual); var product = FindProductById(xmlProduct.ProductId); product.Json = json; _context.Product.Update(product); _context.SaveChanges(); return; }