c# - XPath: Select from non-root node fails -
I am trying to start with nodes that are not the root nodes of XML documents The following code: < Pre> The XML document (here is only one snippet) is the following: But the above code does not work: xmlNode The variable is always empty ... what's wrong? Leading "/" means "starting with the root of the document". Just try: (If you are using .NET 3.5 or later I would rather use LINQ instead of XML - I find it easy to use XPath, but this one If you want the XML code to the same LINQ as you want.) Edit: Understand your This works: You may note, it is still valid that foreach (XmlNode xmlIter .ctlect nodes ("/ refresher / refact1 [[id = 'parameter'] / variables / *")) {parameter p = new parameter (); XmlNode = xmlIter.SelectSingleNode ("varlistentry / word / parameter"); P.Identifier = xmlNode.InnerText; XmlNode = xmlIter.SelectSingleNode ("varlistentry / listitem"); P.Documentation = xmlNode.InnerText; }
& lt; Refsect1 id = "parameter" & gt; & Lt; Title & gt; Parameters & lt; / Title & gt; & Lt; Variablelist & gt; & Lt; Varlistentry & gt; & Lt; Period & gt; & Lt; Parameter & gt; Pipeline & lt; / Parameters & gt; & Lt; / Period & gt; & Lt; ListItem & gt; & Lt; Paragraph & gt; & Lt; / Paragraph & gt; & Lt; / ListItem & gt; & Lt; / Varlistentry & gt; & Lt; Varlistentry & gt; & Lt; Period & gt; & Lt; Parameter & gt; Program & lt; / Parameters & gt; & Lt; / Period & gt; & Lt; ListItem & gt; & Lt; Paragraph & gt; & Lt; / Paragraph & gt; & Lt; / ListItem & gt; & Lt; / Varlistentry & gt; & Lt; / Variablelist & gt; & Lt; / Refsect1 & gt;
xmlNode = xmlIter.SelectSingleNode ("varlistentry / word / parameter"); P.Identifier = xmlNode.InnerText; XmlNode = xmlIter.SelectSingleNode ("varlistentry / term / listitem"); P.Documentation = xmlNode.InnerText;
[stuff] / variablelist / * query already in the
Varlistentry is going to the node - then you are trying to find another
varlistentry node below if you want to replace it with all
varli Stentry elements, then you only
word / parameter and
listitem elements
varlistentry .
xmlNode is non-empty before using it.
Comments
Post a Comment