Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
    <name>Belgian Waffles
    <price>$5.95
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   
    <calories>650

<food>
    <name>Strawberry Belgian Waffles
    <price>$7.95
    <description>
    Light Belgian waffles covered with strawberries and whipped cream
    
    <calories>900

<food>
    <name>Berry-Berry Belgian Waffles
    <price>$8.95
    <description>
    Belgian waffles covered with assorted fresh berries and whipped cream
    
    <calories>900


my output
Belgian Waffles                my xPath== "breakfast_menu/food/name"
Strawberry Belgian Waffles     my xPath== "breakfast_menu/food/name"
Berry-Berry Belgian Waffles    my xPath== "breakfast_menu/food/name"


but i want output like
Belgian Waffles                my xPath== "breakfast_menu/food[1]/name"
Strawberry Belgian Waffles     my xPath== "breakfast_menu/food[2]/name"
Berry-Berry Belgian Waffles    my xPath== "breakfast_menu/food[3]/name"


What I have tried:

using (XmlReader reader = XmlReader.Create(pathMain)) 
            {
                XmlNode nodeTemp;
                var elements = new Stack<string>();
                var pathX = string.Empty;
                var pathXTemp = string.Empty;
                string prevNodeType = string.Empty;
                string currNodeType = string.Empty;
                while (reader.Read())
                {

                    string dg = string.Empty;

                    if (!reader.IsEmptyElement == true)
                    {
                        currNodeType = reader.Name.ToString();
                        if ((reader.NodeType.ToString() == "EndElement") || (prevNodeType != currNodeType))
                        {
                            indx = 1;
                        }
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                elements.Push(reader.LocalName);
                               
                                addtoStackPanel(reader.Name, reader.Value, pathMain, pathX, 0);
                                break;
                            case XmlNodeType.Text:
                                if ((reader.Value != "") && (reader.Name != "style"))
                                {
                                   

                                    pathX = string.Join("/", elements.Reverse());
                                    
                                    addtoStackPanel(reader.Name, reader.Value, pathMain, pathX, 0);
                                    //prevNodeType = reader.Name.ToString();
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                           
                            case XmlNodeType.EndElement:
                                elements.Pop();
                                break;
                        }

                    }
                  
                }
            }
Posted
Updated 4-Jun-18 2:50am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900