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> 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; }

The XML document (here is only one snippet) is the following:

  & 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;   

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:

  xmlNode = xmlIter.SelectSingleNode ("varlistentry / word / parameter"); P.Identifier = xmlNode.InnerText; XmlNode = xmlIter.SelectSingleNode ("varlistentry / term / listitem"); P.Documentation = xmlNode.InnerText;   

(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 [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 .

This works: Precam (xmlNode xmlIter ("/ refentry / refsect1 [@ id = 'parameter'] / variables / varlistentry" in doc.SelectNodes ) {XmlNode xmlNode = xmlIter.SelectSingleNode ("word / parameter"); Console.light line ("identifier = {0}", xmlNode.InnerText); XmlNode = xmlIter.SelectSingleNode ("list"); Console.light line ("documentation = {0}", xmlNode.InnerText); }

You may note, it is still valid that xmlNode is non-empty before using it.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -