xpath and select element

Last post 07-10-2008 2:57 AM by NathanC. 4 replies.

Sort Posts:

  • xpath and select element

    07-04-2008, 3:36 AM
    • Loading...
    • GillouX
    • Joined on 06-03-2007, 3:28 PM
    • Luxemburg
    • Posts 381

     Hello

    I have an xml:

    <applis>

      <appli>

         <name></name>
        <path></path>

     </appli>

      <appli>

         <name></name>
        <path></path>

     </appli>

    </applis>

     

     

    I'm using xpath to iterate inside the applis

     

             XPathDocument doc = new XPathDocument(XMLHelper.localpathXML);
                XPathNavigator nav = doc.CreateNavigator();
    
                XPathExpression expr;
                expr = nav.Compile("/applis/appli/");
                XPathNodeIterator iterator = nav.Select(expr);
    
                try
                {
                    while (iterator.MoveNext())
                    {
                      //some code
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
     
    What I want to do, is that inside de loop while (//some code) , I want to get the element of the iterator
    so basically I would like to make sg liek iterator.getelement("name") to get the name node.
    do you know if it's possible to do this ?
     
    Hope it's clear. 
    Thx  
     
      
  • Re: xpath and select element

    07-04-2008, 9:19 AM
    Answer
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 4:27 PM
    • MN, USA
    • Posts 3,102
    • Moderator
      TrustedFriends-MVPs

    I think the XPath is incorrect, try this:

            expr = nav.Compile("/applis/appli");
            XPathNodeIterator iterator = nav.Select(expr);
    
            try
            {
                while (iterator.MoveNext())
                {
                    Response.Write(iterator.Current.Name  + "<br />");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

     Here is an alternate way:

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(MapPath(@"~\Test.xml"));
    
            XmlNodeList NodeList = xDoc.SelectNodes("/applis/appli");
            foreach (XmlNode XNode in  NodeList)
                Response.Write(XNode.Name + "<br />");
     

    This site has a free tool for building XPath Expressions (XPath Builder):  Bubba Soft

     
    Steve Wellens
  • Re: xpath and select element

    07-09-2008, 9:35 AM
    • Loading...
    • NathanC
    • Joined on 05-25-2008, 6:43 AM
    • Posts 14

    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;
  • Re: xpath and select element

    07-09-2008, 11:12 PM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 4:27 PM
    • MN, USA
    • Posts 3,102
    • Moderator
      TrustedFriends-MVPs

    NathanC:
    Is there any reaon why an XPath expression may not work?

    Since XPath is used to select nodes, it must traverse the entire document. 

    With an extremely large document, there could be trouble...Wink but I don't know what specific problems can occur.

     

    Steve Wellens
  • Re: xpath and select element

    07-10-2008, 2:57 AM
    • Loading...
    • NathanC
    • Joined on 05-25-2008, 6:43 AM
    • Posts 14

    I see, that makes sense,

    Many thanks,

    NathanC

    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;
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter