Expression must evaluate to a node-set.

Last post 05-11-2008 10:32 AM by canalso. 6 replies.

Sort Posts:

  • Expression must evaluate to a node-set.

    05-07-2008, 12:20 AM
    • Loading...
    • canalso
    • Joined on 03-19-2008, 12:49 AM
    • Posts 183

    I try to retrieve XML CDATA Section but it throws the following error:

    • Expression must evaluate to a node-set.  

    webform.aspx

    Protected Sub Page_Load(ByVal sender As Object, _

    ByVal e As System.EventArgs)

    Dim objChangeLang As ChangeLang2.XmlLang02 = New ChangeLang2.XmlLang02

     

    objChangeLang.XmlLang2(
    "aboutus1.xml", "Eng")asdf1.Text = objChangeLang.GetString2("Xmlabout0001")

     

    End Sub

    <asp:Label id="asdf1" runat="server" />

    aboutus1.xml 

    <
    languages>

    <language lang="ENG" engname="English" langname="English" charset="Windows-1252">

    <field id="Xmlabout0001" taborder="1">

    <field_value>

    <![CDATA[About Us]]>

    </field_value>

    </field>

    testlanguage.vb

    Namespace ChangeLang2

    Public Class XmlLang02

    Inherits System.Web.UI.Page

    Protected X As XmlDataDocument

    Protected Lang As String

    Public Sub XmlLang2(ByVal FileName As String, ByVal LanguageValue As String)

    Dim Name As String

    Name = "http://192.168.33.188:8008/la4/" & FileNameX = New XmlDataDocument()

    X.Load(Name)

    Lang = LanguageValue

    End Sub

    Public Sub XmlLang2(ByVal FileName As String)

    Dim Name As String

    Name = "http://192.168.33.188:8008/la4/" & FileNameX = New XmlDataDocument()

    X.Load(Name)

    Lang = "ENG"

    End Sub

    Public Function GetString2(ByVal Name As String) As String

    Dim result As String = Nothing

    Dim root As XmlElement = X.DocumentElement

    Dim node As XmlNode = X.DocumentElement.SelectSingleNode("//languages/language[@lang='" & Lang & "']/field[@id='" & Name & "']/field_value/")           <---- Expression must evaluate to a node-set.

    Dim childNode As XmlNode = node.ChildNodes(0)

    If TypeOf childNode Is XmlCDataSection Then

    Dim cdataSection As XmlCDataSection = TryCast(childNode, XmlCDataSection)

    Dim value As String

    'result = Convert.FromBase64String(cdataSection.Value)

    value = CStr(cdataSection.Value)

    result = value

    End If

    GetString2 = result

    End Function

    End Class

    . . : : LarnVok : : . .
  • Re: Expression must evaluate to a node-set.

    05-07-2008, 9:01 AM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 9:27 PM
    • MN, USA
    • Posts 2,286
    • Moderator
      TrustedFriends-MVPs

    canalso:

    Dim node As XmlNode = X.DocumentElement.SelectSingleNode("//languages/language[@lang='" & Lang & "']/field[@id='" & Name & "']/field_value/")           <---- Expression must evaluate to a node-set.

    I think you have an extra forward slash at the end of your search string:   ..../field_value

     

    Steve Wellens
  • Re: Expression must evaluate to a node-set.

    05-09-2008, 12:13 AM
    • Loading...
    • canalso
    • Joined on 03-19-2008, 12:49 AM
    • Posts 183

    It throws the following error :

    •  Object reference not set to an instance of an object.

    Public Function GetString2(ByVal Name As String) As String

    Dim result As String = Nothing

    Dim root As XmlElement = X.DocumentElement

    Dim node As XmlNode = X.DocumentElement.SelectSingleNode("//languages/language[@lang='" & Lang & "']/field[@id='" & Name & "']/field_value")       

    Dim childNode As XmlNode = node.ChildNodes(0)         <---- Object reference not set to an instance of an object.

    If TypeOf childNode Is XmlCDataSection Then

    Dim cdataSection As XmlCDataSection = TryCast(childNode, XmlCDataSection)

    Dim value As String

    'result = Convert.FromBase64String(cdataSection.Value)

    value = CStr(cdataSection.Value)

    result = value

    End If

    GetString2 = result

    End Function

    End Class

     

    . . : : LarnVok : : . .
  • Re: Expression must evaluate to a node-set.

    05-09-2008, 9:22 AM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 9:27 PM
    • MN, USA
    • Posts 2,286
    • Moderator
      TrustedFriends-MVPs

    You are passing in "Name" and using it in two places in your XPath query.  The XML does not have any matching nodes for the query you are building.

    This retrieves a node with child nodes:

       //languages/language[@lang="ENG"]/field[@id="Xmlabout0001"]/field_value

     

    Bubba Soft This site has a great free tool for building XPath Expressions (XPath Builder).

     

    Steve Wellens
  • Re: Expression must evaluate to a node-set.

    05-11-2008, 5:55 AM
    • Loading...
    • canalso
    • Joined on 03-19-2008, 12:49 AM
    • Posts 183
    I think the 'Public Sub XmlLang2' already stored the 'Lang' value after executing this command:

    objChangeLang.XmlLang2("aboutus1.xml", "Eng")

     

    I modified by using the following code and it argues the following line:

    Dim root As XmlElement = X.DocumentElement

    Dim node As XmlNode = X.DocumentElement.SelectSingleNode("//languages/language[@lang='" & Lang & "']")

    For Each x In node.childNodes

    If Name = x.getelementbyid("id") Then           < ------ BC30518: Overload resolution failed because no accessible '=' can be called with these arguments:

    If Not (x.childNodes(0).childNodes(0) Is Nothing) Then

    getstring2 = x.childNodes(0).childNodes(0).nodevalue

    Else

    getstring2 = ""

    End If

    End If

    Next

     

    . . : : LarnVok : : . .
  • Re: Expression must evaluate to a node-set.

    05-11-2008, 7:43 AM
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 9:27 PM
    • MN, USA
    • Posts 2,286
    • Moderator
      TrustedFriends-MVPs

    canalso:
    If Name = x.getelementbyid("id") Then          

    Name is a string, GetElementById returns an HtmlElement.  You can cannot compare things of different types.

    HtmlElement has InnerText and InnerHtml properties, perhaps you can use one of those.

    Steve Wellens
  • Re: Expression must evaluate to a node-set.

    05-11-2008, 10:32 AM
    • Loading...
    • canalso
    • Joined on 03-19-2008, 12:49 AM
    • Posts 183

    I have a go on the search on a method which substitutes the x.getAttribute, and this might be the closest answer. I would like to know more about InnerText and InnerHtml which might applicable in my scenario. 

    . . : : LarnVok : : . .
Page 1 of 1 (7 items)