XMLDocument.SelectSingleNode() not working?

J

juvi

Hello,

I want change my application to support language files from xml and I
created a language.xml - but my problem is now that I am not able to get a
specific element value back from a node (wanted to use XPath):

xml looks like this:

<language>
<MainDialog>
<panel>
<title>Main Panel</title>
</panel>
</MainDialog>
</language>

I loaded the document with an XmlTextReader to a XmlDocument and wanted to
use the following which has unfortunately no value:

doc.SelectSingleNode("/language/MainDialog/panel").Attributes["title"].Value.ToString();

What am I doing wrong?? Thanks for any help in advance.

BR, juvi
 
J

juvi

Thx for reply....ok....if I change the xpath to:

"/language/MainDialog/panel/title"

which method throws me the wanted value back? (I am sorry for this basic
question but I a completly new to System.XML and the documentation did not
help for me)

juvi
 
J

juvi

no matter what I try ... I do not get any value back...only an empty string??
looking forward to your reply...

juvi
 
C

Christopher Fairbairn [MVP]

Hi,

juvi said:
Thx for reply....ok....if I change the xpath to:
"/language/MainDialog/panel/title"
which method throws me the wanted value back?

How about XmlNode's InnerText property, for example:

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<language>
<MainDialog>
<panel>
<title>Main Panel</title>
</panel>
</MainDialog>
</language>");
XmlNode node = doc.SelectSingleNode("/language/MainDialog/panel/title");
Debug.WriteLine(node.InnerText);

Hope this helps,
Christopher Fairbairn
 
J

juvi

thx for reply.... I tried it now but again: the result is empty when trying
to select through XPath....if going down with nodeindexes then it works ??
strange ... would be happy to get it work with XPath

XNode = doc.SelectSingleNode("/language/MainDialog/panel/title");
s = XNode.InnerText.ToString(); // does not work

s = doc.ChildNodes.Item(0).ChildNodes.Item(0).FirstChild.InnerText; // works

anybody knows what the problem could be?


thx
 
J

juvi

Now it works....thank you all....there was a problem with my xmlreader
code...thx juvi
 
S

Simon Hart [MVP]

If you're using CF 3.5 you could use LINQ to XML - it's alot easier than
XPath.
 

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