Why is this XPath doesn't give correct result

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Hello!

In the code below I have a n xml file and a piece of code.

In the code is the context node Invoice and I try this
XmlNodeList nodeList = mCurrentNode.SelectNodes("text()");
but it doesn't return the element value of the child of the Invoice as I had
expected why!


Here is the xml document
<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<Date>2004-01-02</Date>
<Item quantity="4">QD123</Item>
<Item quantity="5">AC123</Item>
<Item>"4"</Item>
</Invoice>

public Form1()
{
InitializeComponent();
XmlDocument mDocument = new XmlDocument();
mDocument.Load(@"c:\users\tony\documents\visual studio
2010\Projects\chapter23XPath\chapter23XPath\XPathQuery.xml");
XmlNode mCurrentNode = mDocument.DocumentElement;
XmlNodeList nodeList = mCurrentNode.SelectNodes("text()");
}

//Tony
 
In the code below I have a n xml file and a piece of code.

In the code is the context node Invoice and I try this
XmlNodeList nodeList = mCurrentNode.SelectNodes("text()");
but it doesn't return the element value of the child of the Invoice as I
had expected why!


Here is the xml document
<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<Date>2004-01-02</Date>
<Item quantity="4">QD123</Item>
<Item quantity="5">AC123</Item>
<Item>"4"</Item>
</Invoice>

public Form1()
{
InitializeComponent();
XmlDocument mDocument = new XmlDocument();
mDocument.Load(@"c:\users\tony\documents\visual studio
2010\Projects\chapter23XPath\chapter23XPath\XPathQuery.xml");
XmlNode mCurrentNode = mDocument.DocumentElement;
XmlNodeList nodeList = mCurrentNode.SelectNodes("text()");
}

text() return the text of an element.

Invoice does not have any text.

Try:

mDocument.Load(@"c:\users\tony\documents\visual studio
2010\Projects\chapter23XPath\chapter23XPath\XPathQuery.xml");
XmlNodeList nodeList = mCurrentNode.SelectNodes("/Invoice/Item/text()");

Arne
 
Back
Top