Hi SGWellens,
Is there any reaon why an XPath expression may not work? The Buba XPathBuilder is not equipped to load one of m xml files (100,000 + lines), it tries for several minutes and then crashes. Yet I need to access chil/child/nodes if you like. An example of what i'm trying to accomplish is below:
1 <product product-id="048002274">
2 <display-name>Cotton crew-neck printed T-shirt</display-name>
3 <long-description>Superdry cotton short-sleeved T-shirt with Osaka 6 print emblazoned across chest.</long-description>
4 <online-flag>false</online-flag>
5 <available-flag>true</available-flag>
6 <searchable-flag>true</searchable-flag>
7 <tax-class-id>default</tax-class-id>
8 <brand>Superdry</brand>
9 <manufacturer-name>Superdry</manufacturer-name>
10 <custom-attributes>
11 <custom-attribute attribute-id="Calendar Event">N/A</custom-attribute>
12 <custom-attribute attribute-id="Care Instructions">Machine Wash 30°C</custom-attribute>
13 <custom-attribute attribute-id="Colour">Navy</custom-attribute>
14 <custom-attribute attribute-id="Cuff Type">N/A</custom-attribute>
15 <custom-attribute attribute-id="Exclusive">N/A</custom-attribute>
16 <custom-attribute attribute-id="Fabric">Cotton</custom-attribute>
17 <custom-attribute attribute-id="Fair Trade">N/A</custom-attribute>
18 <custom-attribute attribute-id="Gender">Men</custom-attribute>
19 <custom-attribute attribute-id="Guarantee">N/A</custom-attribute>
20 <custom-attribute attribute-id="House of Fraser Magazine">No</custom-attribute>
21 <custom-attribute attribute-id="Organic">No</custom-attribute>
22 <custom-attribute attribute-id="Parent Colour">Navy</custom-attribute>
23 <custom-attribute attribute-id="Parent Style">TOPS</custom-attribute>
24 <custom-attribute attribute-id="Range">PRE-SEASON/TRANSITIONAL</custom-attribute>
25 <custom-attribute attribute-id="Recycled">N/A</custom-attribute>
26 <custom-attribute attribute-id="Size">S</custom-attribute>
27 <custom-attribute attribute-id="Style">TOP- T-SHIRT</custom-attribute>
28 <custom-attribute attribute-id="Television Line">No</custom-attribute>
29 <custom-attribute attribute-id="Web Supply Lane">iForce Deliveries</custom-attribute>
30 <custom-attribute attribute-id="qosListID">QOS1</custom-attribute>
31 <custom-attribute attribute-id="sellByUnit">false</custom-attribute>
32 <custom-attribute attribute-id="COMPOSITION">100% Cotton</custom-attribute>
33 <custom-attribute attribute-id="PrimaryImage">I_048002282_00_20070621</custom-attribute>
34 </custom-attributes>
35 </product>
My main goal is to get this into a csv file for SQL compatability in the end up; i am doing this using a textWriter. I have got as far as getting all the imediate descendant elements into their correct column, i.e.
product-id|display-name|long-description
What I need to be able to do is add the first child nodes of product to the csv file under their correct column (achieved) but also check the custom-attribute, attribute-id and if it exists then add it to the correct column on the correct line with it's InnerText value.
Below is as close as I have gotten to accomplishing this without XPathExpressions
1 int id = 0;
2
3 string CSVFile = "Prods.txt";
4 TextWriter tw = new StreamWriter(CSVFile);
5 tw.WriteLine("id|product-id|ean|display-name|long-description|online-flag|available-flag|" +
6 "searchable-flag|tax-class-id|brand|manufacturer-name|Calendar Event|Care Instructions|Colour|" +
7 "Cuff Type|Exclusive|Fabric|Fair Trade|Gender|Guarantee|House of Fraser Magazine|Organic|Parent Colour|" +
8 "Parent Style|Range|Recycled|Size|Style|Television Line|Web Supply Lane|qosListID|sellByUnit|COMPOSITION|" +
9 "PrimaryImage|SecondaryImage1|SecondaryImage2|Sizing|Finish|Material|Skin Type|Animal Tested|" +
10 "WEIGHT|UnitMeasure|UnitQty|UnitQtyPrd|SecondaryImage3|SecondaryImage4|SecondaryImage5");
11
12 XmlDocument hofProds = new XmlDocument();
13 hofProds.Load("Catalog.xml");
14
15 XmlNodeList productList = null;
16 productList = hofProds.GetElementsByTagName("product");
17
18 foreach (XmlNode n in productList)
19 {
20 id = id + 1;
21 string ean = "null";
22 string displayName = "null";
23 string longDescription = "null";
24 string onlineFlag = "null";
25 string availableFlag = "null";
26 string searchableFlag = "null";
27 string taxClass = "null";
28 string brand = "null";
29 string manufacturerName = "null";
30
31 if (n["ean"] != null) { ean = n["ean"].InnerText; }
32
33 if (n["display-name"] != null) { displayName = n["display-name"].InnerText; }
34
35 if (n["long-description"] != null) { longDescription = n["long-description"].InnerText; }
36
37 if (n["online-flag"] != null) { onlineFlag = n["online-flag"].InnerText; }
38
39 if (n["available-flag"] != null) { availableFlag = n["available-flag"].InnerText; }
40
41 if (n["searchable-flag"] != null) { searchableFlag = n["searchable-flag"].InnerText; }
42
43 if (n["tax-class-id"] != null) { taxClass = n["tax-class-id"].InnerText; }
44
45 if (n["brand"] != null) { brand = n["brand"].InnerText; }
46
47 if (n["manufacturer-name"] != null) { manufacturerName = n["manufacturer-name"].InnerText; }
48
49 string calendar = "null"; string care = "null"; string colour = "null"; string cuff = "null"; string exclusive = "null";
50 string fabric = "null"; string fairtrade = "null"; string gender = "null"; string guarantee = "null";
51 string hofmagazine = "null"; string organic = "null";
52 string parentColour = "null"; string parentStyle = "null"; string range = "null"; string recycled = "null";
53 string size = "null"; string style = "null"; string tvLine = "null"; string webSupplyLane = "null"; string qosListID = "null";
54 string sellByUnit = "null";
55 string composition = "null"; string primaryImage = "null"; string SecondaryImage1 = "null"; string SecondaryImage2 = "null"; string Sizing = "null";
56 string Finish = "null"; string Material = "null"; string SkinType = "null"; string AnimalTested = "null";
57 string WEIGHT = "null"; string UnitMeasure = "null"; string UnitQty = "null"; string UnitQtyPrd = "null";
58 string SecondaryImage3 = "null"; string SecondaryImage4 = "null"; string SecondaryImage5 = "null";
59
60 XmlNodeList attributeList = n.SelectNodes("custom-attributes/custom-attribute");
61 foreach (XmlNode a in attributeList)
62 {
63 if (a.Attributes["attribute-id"].Value == "Calendar Event") { calendar = a.InnerText; }
64 }
65
66 tw.WriteLine(id + "|" + n.Attributes["product-id"].Value + "|" + ean + "|" + displayName + "|" +
67 longDescription + "|" + onlineFlag + "|" +
68 availableFlag + "|" + searchableFlag + "|" +
69 taxClass + "|" + brand + "|" +
70 manufacturerName + "|" + calendar + "|" + care + "|" + colour + "|" + cuff + "|" + exclusive + "|" + fabric + "|" +
71 fairtrade + "|" + gender + "|" + guarantee + "|" + hofmagazine + "|" + organic + "|" + parentColour + "|" +
72 parentStyle + "|" + range + "|" + recycled + "|" + size + "|" + style + "|" + tvLine + "|" +
73 webSupplyLane + "|" + qosListID + "|" + sellByUnit + "|" + composition + "|" +
74 primaryImage + "|" + SecondaryImage1 + "|" + SecondaryImage2 + "|" + Sizing + "|" +
75 Finish + "|" + Material + "|" + SkinType + "|" + AnimalTested + "|" + WEIGHT + "|" + UnitMeasure + "|" +
76 UnitQty + "|" + UnitQtyPrd + "|" + SecondaryImage3 + "|" + SecondaryImage4 + "|" + SecondaryImage5);
77 }
78 tw.Close();
79 System.Diagnostics.Process.Start("C:\\Users\\nathan.CHANNON\\Documents\\HOFProds.txt");
This though only omits all attribute values wih "null"...
Any help is appreciated,
Many Thanksm
Nathan C
Please mark as the answer if your problem has been resolved as a result of my posting - Ta
Price is what you pay; value is what get;