XMLDocument.SelectSingleNode() not working?

Discussion in 'Microsoft Dot NET Compact Framework' started by juvi, Feb 1, 2009.

  1. juvi

    juvi Guest

    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
     
    juvi, Feb 1, 2009
    #1
    1. Advertisements

  2. Title is not an attribute, it's a child node.


    --

    Chris Tacke, Embedded MVP
    OpenNETCF Consulting
    Giving back to the embedded community
    http://community.OpenNETCF.com

    "juvi" <> wrote in message
    news:...
    > 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
     
    Chris Tacke, eMVP, Feb 1, 2009
    #2
    1. Advertisements

  3. juvi

    juvi Guest

    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
     
    juvi, Feb 1, 2009
    #3
  4. juvi

    juvi Guest

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

    juvi
     
    juvi, Feb 1, 2009
    #4
  5. Hi,

    "juvi" <> wrote in message
    news:...
    > 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
     
    Christopher Fairbairn [MVP], Feb 2, 2009
    #5
  6. juvi

    juvi Guest

    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
     
    juvi, Feb 2, 2009
    #6
  7. juvi

    juvi Guest

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

    "Christopher Fairbairn [MVP]" wrote:

    > Hi,
    >
    > "juvi" <> wrote in message
    > news:...
    > > 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
    >
    >
    >
     
    juvi, Feb 8, 2009
    #7
  8. If you're using CF 3.5 you could use LINQ to XML - it's alot easier than
    XPath.
    --
    Simon Hart
    Visual Developer - Device Application Development MVP
    http://www.simonrhart.com

    "juvi" <> wrote in message
    news:...
    > Now it works....thank you all....there was a problem with my xmlreader
    > code...thx juvi
    >
    > "Christopher Fairbairn [MVP]" wrote:
    >
    >> Hi,
    >>
    >> "juvi" <> wrote in message
    >> news:...
    >> > 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
    >>
    >>
    >>
     
    Simon Hart [MVP], Feb 15, 2009
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Justin

    Slowness of XmlDocument Class

    Justin, Oct 22, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    2
    Views:
    306
    Hollywood
    Oct 28, 2003
  2. Yechezkal Gutfreund

    XmlDocument read from Network Stream

    Yechezkal Gutfreund, Nov 19, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    5
    Views:
    395
    Jon Skeet [C# MVP]
    Nov 19, 2003
  3. César A. Fong Espinola

    LoadXML in XMLDocument

    César A. Fong Espinola, Dec 8, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    4
    Views:
    976
    Alex Feinman [MVP]
    Dec 11, 2003
  4. Guest

    CF-XMLDocument: XLink-Support?

    Guest, Dec 12, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    0
    Views:
    370
    Guest
    Dec 12, 2003
  5. Richard

    How to select a Node in an XMLDocument?

    Richard, May 1, 2004, in forum: Microsoft Dot NET Compact Framework
    Replies:
    2
    Views:
    1,180
    Zordiac
    May 3, 2004
  6. Dan

    How to implement SelectSingleNode in Compact Framework

    Dan, Sep 12, 2004, in forum: Microsoft Dot NET Compact Framework
    Replies:
    3
    Views:
    1,089
    Chris Tacke, eMVP
    Sep 12, 2004
  7. Safae

    XmlTextReader or XmlDocument or SQLCE

    Safae, Jan 5, 2006, in forum: Microsoft Dot NET Compact Framework
    Replies:
    6
    Views:
    444
    Ilya Tumanov [MS]
    Jan 10, 2006
  8. Steven Edison

    XmlDocument and SelectSingleNode

    Steven Edison, Jan 20, 2010, in forum: Microsoft Dot NET Compact Framework
    Replies:
    1
    Views:
    1,263
    Steven Edison
    Jan 20, 2010
Loading...