Hi,
ich möchte in einem XML n Knoten finden und in jedem gefundenen Knoten wiederum Elemente finden. Es soll XPath zum Einsatz kommen.
Das Iterieren über den xPathNodeIterator klappt famos. Innerhalb der Schleife erzeuge ich einen neuen Navigator (innerXPathNavigator) und lasse darüber wiederum ein Select fahren. Hier fängt mein Problem an:
Das XML innerhalb des innerXPathNavigator entspricht dem gefundenen XML-Knoten. Es sind nur die Daten vorhanden, die meiner xPathExpression entsprechen.
Wenn ich nun aber darauf meine (neue) innerXPathExpression laufen lasse, bekommen ich nicht die "Description" des innerXPathNavigator zurück (die auch existiert), sondern die meines gesamten XML, also des xPathNodeIterator.
Bei folgenden - fiktiven, verkürzt dargestellten - XML-Daten käme als Wert "a" und nicht, wie gewünscht "b1" zurück.
xPathExpression = "//node[@id='id1' and @class='class1']";
xPathNodeIterator = xPathNavigator.Select(xPathExpression);
while (xPathNodeIterator.MoveNext())
{
XPathNavigator innerXPathNavigator = xPathNodeIterator.Current;
string innerXPathExpression = "//description";
XPathNodeIterator innerXPathNodeIterator = innerXPathNavigator.Select(innerXPathExpression);
innerXPathNodeIterator.MoveNext();
string theResult = innerXPathNodeIterator.Current.ToString();
}
<description>a</desciption>
<node>
<description>b1</desciption>
<node>
<node>
<description>b2</desciption>
<node>
<node>
<description>b3</desciption>
<node>
Wo ist das wohl strubblig? Entweder ich verstehe XPath nicht, oder ich sehe den Wald vor lauter Bäumen nicht? Kann jemand helfen?
Vielen Dank im voraus,
🙂 Torsten
ich nehm mal an, das sollte so heißen,
<Stammelement>
<description>a</desciption>
<node>
<description>b1</desciption>
<node>
<node>
<description>b2</desciption>
<node>
<node>
<description>b3</desciption>
<node>
</Stammelement>
wenn du z.B. alle nodes von description haben willst, dann:
//Stammelement/description/*/description
damit bekommst du alle description nodes, vorausgesetzt es ist kein Namespace enthalten im XML
Seit der Erkenntnis, dass der Mensch eine Nachricht ist, erweist sich seine körperliche Existenzform als überflüssig.
Hi dr4g0n76,
danke für Deine Antwort. Die Ergebnismenge ist für meine Logik allerdings nicht so gut brauchbar. Ich benötige alle Elemente n einer Node innerhalb einer Iteration.
Lustig ist, dass wenn ich mit meinem OuterXML meines äußeren Iterators ein neues XPathDocument erzeuge und danach wie gehabt navigiere - dann geht's ... 😕
Komisch,
🙂 Torsten
Dann denke ich solltest Du die Abfragen mit XPath genauer anschauen, normalerweise hat mir bisher immer EINE Abfrage gereicht.
s. z.B. hier
unter Kapitel 2. Locations Path:
Auszüge
child::para selects the para element children of the context node
child:😘 selects all element children of the context node
child::text() selects all text node children of the context node
child::node() selects all the children of the context node, whatever their node type
attribute::name selects the name attribute of the context node
attribute:😘 selects all the attributes of the context node
descendant::para selects the para element descendants of the context node
ancestor::div selects all div ancestors of the context node
ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the context node as well
descendant-or-self::para selects the para element descendants of the context node and, if the context node is a para element, the context node as well
self::para selects the context node if it is a para element, and otherwise selects nothing
childchapter/descendantpara selects the para element descendants of the chapter element children of the context node
child::*/child::para selects all para grandchildren of the context node
/ selects the document root (which is always the parent of the document element)
/descendant::para selects all the para elements in the same document as the context node
/descendantolist/childitem selects all the item elements that have an olist parent and that are in the same document as the context node
child::para[position()=1] selects the first para child of the context node
child::para[position()=last()] selects the last para child of the context node
Seit der Erkenntnis, dass der Mensch eine Nachricht ist, erweist sich seine körperliche Existenzform als überflüssig.
string innerXPathExpression = "//description";
Sollte diese Zeile nicht
string innerXPathExpression = "/description";
heißen? Also nur mit einem führenden "/" ? ich war der Meinung das "//" das erste Vorkommen im Dokument und nicht des aktuellen Knoten findet.
EDIT:
Oder probiers mal einfach mit
string innerXPathExpression = "description";
also ohne Schrägstrich. Sollte eigentlich gehen