XML combo problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fremenusul

    XML combo problems

    am trying to have XML data show results in a combobox (Which works) and
    when the combobox has a selection, change the text in a label field.

    I am following the video from
    http://msdn.microsoft.com/vbasic/att...l/default.aspx (reading
    XML data) almost step by step (changing to match my xmlfiles, etc)

    Here is an example of my XML
    <products>
    <product>CP-90, T-32</product>
    <company>Carwel l</company>
    <address>Stre et Address</address>
    <city>City</city>
    <state>State</state>
    <zip>Zip</zip>
    </products>

    My load function (which works)

    Dim xmlFile As String = "..\Products.xm l"
    Dim xmlDoc As XmlDocument
    Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim xmlTr As New XmlTextReader(x mlFile)

    While xmlTr.Read
    If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
    XmlNodeType.Ele ment Then
    cmbProducts.Ite ms.Add(xmlTr.Re adString)
    End If
    End While
    xmlTr.Close()

    xmlDoc = New XmlDocument
    xmlDoc.Load(xml File)

    End Sub

    My selection method
    Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
    ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
    DisplayProduct( cmbProducts.Sel ectedIndex + 1)
    End Sub

    Here is where the problem begins

    Sub DisplayProduct( ByVal position As Integer)
    Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
    "/products/product[" & position & "]")
    lblCompany.Text = node.SelectSing leNode("company ").InnerTex t

    End Sub

    Everything compiles fine but when I run and then select a product I get.
    An unhandled exception of type 'System.NullRef erenceException ' occurred in
    Zadig.exe

    Additional information: Object reference not set to an instance of an object.

    The error says it is on line "lblCompany.Tex t =
    node.SelectSing leNode("company ").InnerTex t" but I have no idea what the real
    problem is.

    Please advise.

    Michael

  • fremenusul

    #2
    RE: XML combo problems

    Again, my internet. ... sorry.

    "fremenusul " wrote:
    [color=blue]
    > am trying to have XML data show results in a combobox (Which works) and
    > when the combobox has a selection, change the text in a label field.
    >
    > I am following the video from
    > http://msdn.microsoft.com/vbasic/att...l/default.aspx (reading
    > XML data) almost step by step (changing to match my xmlfiles, etc)
    >
    > Here is an example of my XML
    > <products>
    > <product>CP-90, T-32</product>
    > <company>Carwel l</company>
    > <address>Stre et Address</address>
    > <city>City</city>
    > <state>State</state>
    > <zip>Zip</zip>
    > </products>
    >
    > My load function (which works)
    >
    > Dim xmlFile As String = "..\Products.xm l"
    > Dim xmlDoc As XmlDocument
    > Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    > Dim xmlTr As New XmlTextReader(x mlFile)
    >
    > While xmlTr.Read
    > If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
    > XmlNodeType.Ele ment Then
    > cmbProducts.Ite ms.Add(xmlTr.Re adString)
    > End If
    > End While
    > xmlTr.Close()
    >
    > xmlDoc = New XmlDocument
    > xmlDoc.Load(xml File)
    >
    > End Sub
    >
    > My selection method
    > Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
    > ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
    > DisplayProduct( cmbProducts.Sel ectedIndex + 1)
    > End Sub
    >
    > Here is where the problem begins
    >
    > Sub DisplayProduct( ByVal position As Integer)
    > Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
    > "/products/product[" & position & "]")
    > lblCompany.Text = node.SelectSing leNode("company ").InnerTex t
    >
    > End Sub
    >
    > Everything compiles fine but when I run and then select a product I get.
    > An unhandled exception of type 'System.NullRef erenceException ' occurred in
    > Zadig.exe
    >
    > Additional information: Object reference not set to an instance of an object.
    >
    > The error says it is on line "lblCompany.Tex t =
    > node.SelectSing leNode("company ").InnerTex t" but I have no idea what the real
    > problem is.
    >
    > Please advise.
    >
    > Michael
    >[/color]

    Comment

    • Dennis Myrén

      #3
      Re: XML combo problems

      Michael,
      [color=blue]
      > Here is an example of my XML
      > <products>
      > <product>CP-90, T-32</product>
      > <company>Carwel l</company>
      > <address>Stre et Address</address>
      > <city>City</city>
      > <state>State</state>
      > <zip>Zip</zip>
      > </products>[/color]

      You are trying to select child node <company> of node <product>.
      But product has no children. That is why you get a NullReferenceEx ception.
      I think what you are doing wrong here is the structure of the XML document.
      In your example there was no way to associate a <company> or <address> etc.
      to a product(other than physichal order in file, but do not rely on that).
      I think you should restructure your document in order to look something like
      this:
      <products>
      <!-- Repeat the structure below for each product. -->
      <product>
      <id>CP-90, T-32</id>
      <company>Carwel l</company>
      <address>Stre et Address</address>
      <city>City</city>
      <state>State</state>
      <zip>Zip</zip>
      </product>

      <!-- Next product here-->
      </products>

      That is a logical hierarchy.
      In that example i moved what i think is the product ID into a new <id>
      element.

      Now you should have no problems with the XPath selection.


      --
      Regards,
      Dennis JD Myrén
      Oslo Kodebureau
      "fremenusul " <fremenusul@dis cussions.micros oft.com> wrote in message
      news:51C7AD62-0E03-4BF3-84AD-8F0209375B56@mi crosoft.com...[color=blue]
      > am trying to have XML data show results in a combobox (Which works) and
      > when the combobox has a selection, change the text in a label field.
      >
      > I am following the video from
      > http://msdn.microsoft.com/vbasic/att...l/default.aspx
      > (reading
      > XML data) almost step by step (changing to match my xmlfiles, etc)
      >
      > Here is an example of my XML
      > <products>
      > <product>CP-90, T-32</product>
      > <company>Carwel l</company>
      > <address>Stre et Address</address>
      > <city>City</city>
      > <state>State</state>
      > <zip>Zip</zip>
      > </products>
      >
      > My load function (which works)
      >
      > Dim xmlFile As String = "..\Products.xm l"
      > Dim xmlDoc As XmlDocument
      > Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
      > System.EventArg s) Handles MyBase.Load
      > Dim xmlTr As New XmlTextReader(x mlFile)
      >
      > While xmlTr.Read
      > If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
      > XmlNodeType.Ele ment Then
      > cmbProducts.Ite ms.Add(xmlTr.Re adString)
      > End If
      > End While
      > xmlTr.Close()
      >
      > xmlDoc = New XmlDocument
      > xmlDoc.Load(xml File)
      >
      > End Sub
      >
      > My selection method
      > Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
      > ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
      > DisplayProduct( cmbProducts.Sel ectedIndex + 1)
      > End Sub
      >
      > Here is where the problem begins
      >
      > Sub DisplayProduct( ByVal position As Integer)
      > Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
      > "/products/product[" & position & "]")
      > lblCompany.Text = node.SelectSing leNode("company ").InnerTex t
      >
      > End Sub
      >
      > Everything compiles fine but when I run and then select a product I get.
      > An unhandled exception of type 'System.NullRef erenceException ' occurred in
      > Zadig.exe
      >
      > Additional information: Object reference not set to an instance of an
      > object.
      >
      > The error says it is on line "lblCompany.Tex t =
      > node.SelectSing leNode("company ").InnerTex t" but I have no idea what the
      > real
      > problem is.
      >
      > Please advise.
      >
      > Michael
      >[/color]


      Comment

      • fremenusul

        #4
        Re: XML combo problems

        ITS WORKS!!!!!

        You guys are great!!!!

        "Dennis Myrén" wrote:
        [color=blue]
        > Michael,
        >[color=green]
        > > Here is an example of my XML
        > > <products>
        > > <product>CP-90, T-32</product>
        > > <company>Carwel l</company>
        > > <address>Stre et Address</address>
        > > <city>City</city>
        > > <state>State</state>
        > > <zip>Zip</zip>
        > > </products>[/color]
        >
        > You are trying to select child node <company> of node <product>.
        > But product has no children. That is why you get a NullReferenceEx ception.
        > I think what you are doing wrong here is the structure of the XML document.
        > In your example there was no way to associate a <company> or <address> etc.
        > to a product(other than physichal order in file, but do not rely on that).
        > I think you should restructure your document in order to look something like
        > this:
        > <products>
        > <!-- Repeat the structure below for each product. -->
        > <product>
        > <id>CP-90, T-32</id>
        > <company>Carwel l</company>
        > <address>Stre et Address</address>
        > <city>City</city>
        > <state>State</state>
        > <zip>Zip</zip>
        > </product>
        >
        > <!-- Next product here-->
        > </products>
        >
        > That is a logical hierarchy.
        > In that example i moved what i think is the product ID into a new <id>
        > element.
        >
        > Now you should have no problems with the XPath selection.
        >
        >
        > --
        > Regards,
        > Dennis JD Myrén
        > Oslo Kodebureau
        > "fremenusul " <fremenusul@dis cussions.micros oft.com> wrote in message
        > news:51C7AD62-0E03-4BF3-84AD-8F0209375B56@mi crosoft.com...[color=green]
        > > am trying to have XML data show results in a combobox (Which works) and
        > > when the combobox has a selection, change the text in a label field.
        > >
        > > I am following the video from
        > > http://msdn.microsoft.com/vbasic/att...l/default.aspx
        > > (reading
        > > XML data) almost step by step (changing to match my xmlfiles, etc)
        > >
        > > Here is an example of my XML
        > > <products>
        > > <product>CP-90, T-32</product>
        > > <company>Carwel l</company>
        > > <address>Stre et Address</address>
        > > <city>City</city>
        > > <state>State</state>
        > > <zip>Zip</zip>
        > > </products>
        > >
        > > My load function (which works)
        > >
        > > Dim xmlFile As String = "..\Products.xm l"
        > > Dim xmlDoc As XmlDocument
        > > Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
        > > System.EventArg s) Handles MyBase.Load
        > > Dim xmlTr As New XmlTextReader(x mlFile)
        > >
        > > While xmlTr.Read
        > > If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
        > > XmlNodeType.Ele ment Then
        > > cmbProducts.Ite ms.Add(xmlTr.Re adString)
        > > End If
        > > End While
        > > xmlTr.Close()
        > >
        > > xmlDoc = New XmlDocument
        > > xmlDoc.Load(xml File)
        > >
        > > End Sub
        > >
        > > My selection method
        > > Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
        > > ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
        > > DisplayProduct( cmbProducts.Sel ectedIndex + 1)
        > > End Sub
        > >
        > > Here is where the problem begins
        > >
        > > Sub DisplayProduct( ByVal position As Integer)
        > > Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
        > > "/products/product[" & position & "]")
        > > lblCompany.Text = node.SelectSing leNode("company ").InnerTex t
        > >
        > > End Sub
        > >
        > > Everything compiles fine but when I run and then select a product I get.
        > > An unhandled exception of type 'System.NullRef erenceException ' occurred in
        > > Zadig.exe
        > >
        > > Additional information: Object reference not set to an instance of an
        > > object.
        > >
        > > The error says it is on line "lblCompany.Tex t =
        > > node.SelectSing leNode("company ").InnerTex t" but I have no idea what the
        > > real
        > > problem is.
        > >
        > > Please advise.
        > >
        > > Michael
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Cor Ligthert

          #5
          Re: XML combo problems

          fremenusul.

          Did you know that your XML file looks as a datatable, which is a part of a
          dataset.

          A dataset is much easier to use in combination with a combobox.
          Something as
          dim ds as new dataset
          ds.readxml("myp ath")
          combobox1.datas ource = ds.tables("prod ucts")
          combobox1.displ aymember = "product"
          combobox1.value member = "company"

          Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
          ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
          dim mystring as string = combobox1.selec tedvalue
          End sub

          Just to give you an idea for an alternative

          Cor


          Comment

          Working...