Как получить xpath с индексированием для динамического обновления XML-файла C#
<?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
мой выход
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"
но я хочу, чтобы выход был таким
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"
Что я уже пробовал:
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; } } } }