Why is this XPath doesn't give correct result

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
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top