using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.XPath;
namespace ConsoleXmlReader { public class item { public string category_id { get; set; } public int listing_id { get; set; } public int package_count { get; set; } public int storage { get; set; } public double tax { get; set; } public string storeCode { get; set; } public int item_id { get; set; } public string item_name { get; set; } public string sku { get; set; } public string guiGe { get; set; } public string yanSe { get; set; } public string chiCun { get; set; } public string chanDi { get; set; } public string caiLiao { get; set; } public int merchant_id { get; set; } public string merchant_name { get; set; } public int dc_code { get; set; } public string dc_name { get; set; } public string upc { get; set; } public int sale_mode { get; set; } public int listing_type { get; set; } public int category1_id { get; set; } public string category1_Name { get; set; } public int category2_id { get; set; } public string category2_Name { get; set; } public int category3_id { get; set; } public string category3_Name { get; set; } }
internal class Program { private static void Main(string[] args) { string uri = "http://192.168.122.35:9003/finding?restype=4101";
var lst = new List<item>(); string category_id = String.Empty; int listing_id = 0; int package_count = 0; int storage = 0; double tax = 0; string storeCode = String.Empty; int item_id = 0; string item_name = String.Empty; string sku = String.Empty; string guiGe = String.Empty; string yanSe = String.Empty; string chiCun = String.Empty; string chanDi = String.Empty; string caiLiao = String.Empty; int merchant_id = 0; string merchant_name = String.Empty; int dc_code = 0; string dc_name = String.Empty; string upc = String.Empty; int sale_mode = 0; int listing_type = 0; int category1_id = 0; string category1_Name = String.Empty; int category2_id = 0; string category2_Name = String.Empty; int category3_id = 0; string category3_Name = String.Empty; XmlReader xmlReader = XmlReader.Create(uri);
XPathDocument xPathDocument=new XPathDocument(uri); XDocument xDocument = XDocument.Load(uri);
var queryItem = from items in xDocument.Descendants("item") select items;
foreach (var it in queryItem) { category_id = it.Attribute("category_id").Value;
listing_id = Convert.ToInt32( it.Attribute("listing_id").Value);
package_count = Convert.ToInt32( it.Attribute("package_count").Value);
storage = Convert.ToInt32( it.Attribute("storage").Value);
tax = Convert.ToDouble( it.Attribute("tax").Value);
storeCode = it.Attribute("storeCode").Value;
item_id = Convert.ToInt32( it.Attribute("id").Value);
item_name = it.Element("item_name").Value; sku = it.Element("sku").Value;
foreach (var elementAtrr in it.Descendants("attribute")) { string name = elementAtrr.Attribute("name").Value; switch (name) { case "规格": guiGe = elementAtrr.Element("attr_value").Value; break; case "颜色": yanSe = elementAtrr.Element("attr_value").Value; break; case "尺寸": chiCun = elementAtrr.Element("attr_value").Value; break; case "产地": chanDi = elementAtrr.Element("attr_value").Value; break; case "材质": caiLiao = elementAtrr.Element("attr_value").Value; break; } }
merchant_id = Convert.ToInt32(it.Element("merchant").Attribute("id").Value); merchant_name = it.Element("merchant").Value;
dc_code = Convert.ToInt32(it.Element("dc").Attribute("dc_code").Value); dc_name = it.Element("dc").Value;
upc = it.Element("upc").Value;
sale_mode = Convert.ToInt32(it.Element("sale_mode").Value); listing_type = Convert.ToInt32(it.Element("listing_type").Value);
foreach (var elementcat in it.Descendants("category")) { string level = elementcat.Attribute("level").Value; switch (level) { case "1": category1_id = Convert.ToInt32(elementcat.Attribute("id").Value); category1_Name = elementcat.Value; break; case "2": category2_id = Convert.ToInt32(elementcat.Attribute("id").Value); category2_Name = elementcat.Value; break; case "3": category3_id = Convert.ToInt32(elementcat.Attribute("id").Value); category3_Name = elementcat.Value; break; }
}
lst.Add(new item { category_id = category_id, listing_id = listing_id, package_count = package_count, storage = storage, tax = tax, storeCode = storeCode, item_id = item_id, item_name = item_name, sku = sku, guiGe = guiGe, yanSe = yanSe, chiCun = chiCun, chanDi = chanDi, caiLiao = caiLiao, merchant_id = merchant_id, merchant_name = merchant_name, dc_code = dc_code, dc_name = dc_name, upc = upc, sale_mode = sale_mode, listing_type = listing_type, category1_id = category1_id, category1_Name = category1_Name, category2_id = category2_id, category2_Name = category2_Name, category3_id = category3_id, category3_Name = category3_Name }); }
/* XPathNavigator xPathNavigator = xPathDocument.CreateNavigator(); XPathNodeIterator nodeitemIterator= xPathNavigator.Select("//items/item");
while (nodeitemIterator.MoveNext()) { XPathNavigator pathNavigatorItem= nodeitemIterator.Current; category_id = pathNavigatorItem.GetAttribute("category_id",""); listing_id = Convert.ToInt32(pathNavigatorItem.GetAttribute("listing_id", ""));
package_count = Convert.ToInt32(pathNavigatorItem.GetAttribute("package_count", ""));
storage = Convert.ToInt32(pathNavigatorItem.GetAttribute("storage", ""));
tax = Convert.ToDouble(pathNavigatorItem.GetAttribute("tax", ""));
storeCode = pathNavigatorItem.GetAttribute("storeCode", "");
item_id = Convert.ToInt32(pathNavigatorItem.GetAttribute("id", "")); XPathNodeIterator iteratorItem_name = pathNavigatorItem.SelectChildren("item_name", ""); while (iteratorItem_name.MoveNext()) { item_name = iteratorItem_name.Current.Value; ; }
XPathNodeIterator iteratorsku = pathNavigatorItem.SelectChildren("sku", ""); while (iteratorsku.MoveNext()) { sku = iteratorsku.Current.Value; ; }
XPathNodeIterator iteratorAttr = pathNavigatorItem.Select("attributes/attribute");
while (iteratorAttr.MoveNext()) { string name = iteratorAttr.Current.GetAttribute("name",""); switch (name) { case "规格": guiGe = iteratorAttr.Current.Value; break; case "颜色": yanSe = iteratorAttr.Current.Value; break; case "尺寸": chiCun = iteratorAttr.Current.Value; break; case "产地": chanDi = iteratorAttr.Current.Value; break; case "材质": caiLiao = iteratorAttr.Current.Value; break; } } XPathNodeIterator iteratorMerchant = pathNavigatorItem.Select("merchant"); while (iteratorMerchant.MoveNext()) { merchant_id = Convert.ToInt32(iteratorMerchant.Current.GetAttribute("id", "")); merchant_name = iteratorMerchant.Current.Value; }
XPathNodeIterator iteratordc = pathNavigatorItem.Select("dc"); while (iteratordc.MoveNext()) { dc_code = Convert.ToInt32(iteratordc.Current.GetAttribute("dc_code", "")); dc_name = iteratordc.Current.Value; } XPathNodeIterator iteratorupc = pathNavigatorItem.Select("upc"); while (iteratorupc.MoveNext()) { upc = iteratorupc.Current.Value; }
XPathNodeIterator iteratorsale_mode = pathNavigatorItem.Select("sale_mode"); while (iteratorsale_mode.MoveNext()) { sale_mode = Convert.ToInt32(iteratorsale_mode.Current.Value); }
XPathNodeIterator iteratorlisting_type = pathNavigatorItem.Select("listing_type"); while (iteratorlisting_type.MoveNext()) { listing_type = Convert.ToInt32(iteratorlisting_type.Current.Value); }
XPathNodeIterator iteratorCategory = pathNavigatorItem.Select("category_path/category");
while (iteratorCategory.MoveNext()) { string level = iteratorCategory.Current.GetAttribute("level", ""); switch (level) { case "1": category1_id = Convert.ToInt32(iteratorCategory.Current.GetAttribute("id", "")); category1_Name = iteratorCategory.Current.Value; break; case "2": category2_id = Convert.ToInt32(iteratorCategory.Current.GetAttribute("id", "")); category2_Name = iteratorCategory.Current.Value; break; case "3": category3_id = Convert.ToInt32(iteratorCategory.Current.GetAttribute("id", "")); category3_Name = iteratorCategory.Current.Value; break; } } lst.Add(new item { category_id = category_id, listing_id = listing_id, package_count = package_count, storage = storage, tax = tax, storeCode = storeCode, item_id = item_id, item_name = item_name, sku = sku, guiGe = guiGe, yanSe = yanSe, chiCun = chiCun, chanDi = chanDi, caiLiao = caiLiao, merchant_id = merchant_id, merchant_name = merchant_name, dc_code = dc_code, dc_name = dc_name, upc = upc, sale_mode = sale_mode, listing_type = listing_type, category1_id = category1_id, category1_Name = category1_Name, category2_id = category2_id, category2_Name = category2_Name, category3_id = category3_id, category3_Name = category3_Name }); }*/ /* WebClient webClient=new WebClient(); webClient.Encoding = Encoding.UTF8; string xmlString = webClient.DownloadString(uri); XmlDocument xmlDocument=new XmlDocument(); // xmlDocument.Load(xmlReader); xmlDocument.LoadXml(xmlString);
XmlNodeList nodeList = xmlDocument.SelectNodes("//items/item"); foreach (XmlNode node in nodeList) { foreach(XmlAttribute attribute in node.Attributes) { switch (attribute.Name) { case "category_id": category_id = attribute.Value; break; case "listing_id": listing_id = Convert.ToInt32(attribute.Value); break; case "package_count": package_count = Convert.ToInt32(attribute.Value); break; case "storage": storage = Convert.ToInt32(attribute.Value); break; case "tax": tax = Convert.ToDouble(attribute.Value); break; case "storeCode": storeCode =attribute.Value; break; case "id": item_id = Convert.ToInt32(attribute.Value); break; } } item_name = node.SelectSingleNode("item_name").InnerText; sku = node.SelectSingleNode("sku").InnerText; XmlNodeList nodeListAttr = node.SelectNodes("attributes/attribute"); foreach (XmlNode nodeAttr in nodeListAttr) { switch (nodeAttr.Attributes["name"].Value) { case "规格": guiGe = nodeAttr.FirstChild.InnerText; break; case "颜色": yanSe = nodeAttr.FirstChild.InnerText; break; case "尺寸": chiCun = nodeAttr.FirstChild.InnerText; break; case "产地": chanDi = nodeAttr.FirstChild.InnerText; break; case "材质": caiLiao = nodeAttr.FirstChild.InnerText; break; } }
XmlNode nodeMerchant = node.SelectSingleNode("merchant"); merchant_id = Convert.ToInt32(nodeMerchant.Attributes["id"].Value); merchant_name = nodeMerchant.InnerText;
XmlNode nodedc = node.SelectSingleNode("dc"); dc_code = Convert.ToInt32(nodedc.Attributes["dc_code"].Value); dc_name = nodedc.InnerText;
upc = node.SelectSingleNode("upc").InnerText; sale_mode = Convert.ToInt32(node.SelectSingleNode("sale_mode").InnerText); listing_type = Convert.ToInt32(node.SelectSingleNode("listing_type").InnerText);
XmlNodeList nodeListCategory = node.SelectNodes("category_path/category"); foreach (XmlNode nodeCate in nodeListCategory) { string level= nodeCate.Attributes["level"].Value; switch (level) { case "1": category1_id = Convert.ToInt32(nodeCate.Attributes["id"].Value); category1_Name = nodeCate.InnerText; break; case "2": category2_id = Convert.ToInt32(nodeCate.Attributes["id"].Value); category2_Name = nodeCate.InnerText; break; case "3": category3_id = Convert.ToInt32(nodeCate.Attributes["id"].Value); category3_Name = nodeCate.InnerText; break; } } lst.Add(new item { category_id = category_id, listing_id = listing_id, package_count = package_count, storage = storage, tax = tax, storeCode = storeCode, item_id = item_id, item_name = item_name, sku = sku, guiGe = guiGe, yanSe = yanSe, chiCun = chiCun, chanDi = chanDi, caiLiao = caiLiao, merchant_id = merchant_id, merchant_name = merchant_name, dc_code = dc_code, dc_name = dc_name, upc = upc, sale_mode = sale_mode, listing_type = listing_type, category1_id = category1_id, category1_Name = category1_Name, category2_id = category2_id, category2_Name = category2_Name, category3_id = category3_id, category3_Name = category3_Name }); } */ /* XmlReader xmlReader = XmlReader.Create(uri); while (xmlReader.Read()) { if ( xmlReader.Name == "item") { category_id = xmlReader.GetAttribute("category_id"); listing_id = Convert.ToInt32(xmlReader.GetAttribute("listing_id")); package_count = Convert.ToInt32(xmlReader.GetAttribute("package_count")); storage = Convert.ToInt32(xmlReader.GetAttribute("storage")); tax = Convert.ToDouble(xmlReader.GetAttribute("tax")); storeCode = xmlReader.GetAttribute("storeCode"); item_id = Convert.ToInt32(xmlReader.GetAttribute("id"));
while (xmlReader.Read()) { string elementName= xmlReader.Name; if ( elementName == "item_name") { item_name = xmlReader.ReadString(); } else if ( elementName == "sku") { sku = xmlReader.ReadString(); } else if ( elementName == "attribute") { string name = xmlReader.GetAttribute("name");
if (!String.IsNullOrEmpty(name)) { if (name == "规格") { while (xmlReader.Read()) { elementName = xmlReader.Name; if (elementName == "attr_value") guiGe = xmlReader.ReadString(); else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "attribute") { break; } } } if (name == "颜色") { while (xmlReader.Read()) { elementName = xmlReader.Name; if (elementName == "attr_value") yanSe = xmlReader.ReadString(); else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "attribute") { break; } } }
if (name == "尺寸") { elementName = xmlReader.Name; while (xmlReader.Read()) { if (elementName == "attr_value") chiCun = xmlReader.ReadString(); else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "attribute") { break; } } } if (name == "产地") { while (xmlReader.Read()) { elementName = xmlReader.Name; if (elementName == "attr_value") chanDi = xmlReader.ReadString(); else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "attribute") { break; } } }
if (name == "材质") { while (xmlReader.Read()) { elementName = xmlReader.Name; if (elementName == "attr_value") caiLiao = xmlReader.ReadString(); else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "attribute") { break; } } } } } else if (elementName == "merchant") { if (xmlReader.HasAttributes) merchant_id = Convert.ToInt32(xmlReader.GetAttribute("id")); merchant_name = xmlReader.ReadString(); } else if ( elementName == "dc") { if (xmlReader.HasAttributes) dc_code = Convert.ToInt32(xmlReader.GetAttribute("dc_code")); dc_name = xmlReader.ReadString(); } else if ( elementName == "upc") { upc = xmlReader.ReadString(); } else if ( elementName == "sale_mode") { sale_mode = Convert.ToInt32(xmlReader.ReadString()); } else if ( elementName == "listing_type") { listing_type = Convert.ToInt32(xmlReader.ReadString()); } else if (xmlReader.NodeType == XmlNodeType.EndElement && elementName == "brand") { break; } }
lst.Add(new item { category_id = category_id, listing_id = listing_id, package_count = package_count, storage = storage, tax = tax, storeCode = storeCode, item_id = item_id, item_name = item_name, sku = sku, guiGe = guiGe, yanSe = yanSe, chiCun=chiCun, chanDi = chanDi, caiLiao =caiLiao, merchant_id = merchant_id, merchant_name = merchant_name, dc_code = dc_code, dc_name = dc_name, upc = upc, sale_mode = sale_mode, listing_type = listing_type }); } }*/ } } }
|
请发表评论