Reading XML File

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

    Reading XML File

    Hi

    I'm pretty new at this so please don't laugh too hard. I'm trying to load
    an xml document using VB.NET and having a hard time. My code doesn't crash
    but it doesn't work either, the first msgbox returns 0.

    Help (please!)


    Below is my document and snippet of code

    <?xml version="1.0" encoding="utf-8"?>
    <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
    <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
    </TABLEINFORMATIO N>
    <SERVERS hostname="sta0w p02" database="vivid care">
    </SERVERS>
    <SERVERS hostname="rdy1c n02" database="vivid care">
    </SERVERS>
    </CAREKEEPER>

    CODE:

    Try
    xml_doc.Load(". ./config.xml")
    xml_nodelist = xml_doc.SelectN odes("/carekeeper")

    MsgBox(xml_node list.Count())

    For Each xml_node In xml_nodelist
    server = xml_node.Item(" SERVER").InnerT ext()
    MsgBox(server)
    Next

    Catch ex As Exception
    MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & " "
    & ex.Source)
    End Try
  • TrtnJohn

    #2
    RE: Reading XML File

    What are you actually trying to display?

    There is no "SERVER" node in your XML snippet. Even if you change to access
    "SERVERS" there is no InnerText on any of those entries. (Just attributes).
    Also, why don't you use the .NET Framework instead of the COM XML DOM?

    "Ian" wrote:
    [color=blue]
    > Hi
    >
    > I'm pretty new at this so please don't laugh too hard. I'm trying to load
    > an xml document using VB.NET and having a hard time. My code doesn't crash
    > but it doesn't work either, the first msgbox returns 0.
    >
    > Help (please!)
    >
    >
    > Below is my document and snippet of code
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
    > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
    > </TABLEINFORMATIO N>
    > <SERVERS hostname="sta0w p02" database="vivid care">
    > </SERVERS>
    > <SERVERS hostname="rdy1c n02" database="vivid care">
    > </SERVERS>
    > </CAREKEEPER>
    >
    > CODE:
    >
    > Try
    > xml_doc.Load(". ./config.xml")
    > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
    >
    > MsgBox(xml_node list.Count())
    >
    > For Each xml_node In xml_nodelist
    > server = xml_node.Item(" SERVER").InnerT ext()
    > MsgBox(server)
    > Next
    >
    > Catch ex As Exception
    > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & " "
    > & ex.Source)
    > End Try[/color]

    Comment

    • Ian

      #3
      RE: Reading XML File

      Thanks for the quick response.

      Right now I'm just trying to display the server HOSTNAMES and DATABASE names
      (baby steps). Can you give me a hint to how I use the .NET framework to do
      this?

      Thanks


      "TrtnJohn" wrote:
      [color=blue]
      > What are you actually trying to display?
      >
      > There is no "SERVER" node in your XML snippet. Even if you change to access
      > "SERVERS" there is no InnerText on any of those entries. (Just attributes).
      > Also, why don't you use the .NET Framework instead of the COM XML DOM?
      >
      > "Ian" wrote:
      >[color=green]
      > > Hi
      > >
      > > I'm pretty new at this so please don't laugh too hard. I'm trying to load
      > > an xml document using VB.NET and having a hard time. My code doesn't crash
      > > but it doesn't work either, the first msgbox returns 0.
      > >
      > > Help (please!)
      > >
      > >
      > > Below is my document and snippet of code
      > >
      > > <?xml version="1.0" encoding="utf-8"?>
      > > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
      > > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
      > > </TABLEINFORMATIO N>
      > > <SERVERS hostname="sta0w p02" database="vivid care">
      > > </SERVERS>
      > > <SERVERS hostname="rdy1c n02" database="vivid care">
      > > </SERVERS>
      > > </CAREKEEPER>
      > >
      > > CODE:
      > >
      > > Try
      > > xml_doc.Load(". ./config.xml")
      > > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
      > >
      > > MsgBox(xml_node list.Count())
      > >
      > > For Each xml_node In xml_nodelist
      > > server = xml_node.Item(" SERVER").InnerT ext()
      > > MsgBox(server)
      > > Next
      > >
      > > Catch ex As Exception
      > > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & " "
      > > & ex.Source)
      > > End Try[/color][/color]

      Comment

      • TrtnJohn

        #4
        RE: Reading XML File

        Look in the Visual Studio help file for the class System.Xml.XmlT extReader.
        The help file gives examples of how to use each method in the documentation
        of the method. Some methods you want to look at are: Read and GetAttribute.

        "Ian" wrote:
        [color=blue]
        > Thanks for the quick response.
        >
        > Right now I'm just trying to display the server HOSTNAMES and DATABASE names
        > (baby steps). Can you give me a hint to how I use the .NET framework to do
        > this?
        >
        > Thanks
        >
        >
        > "TrtnJohn" wrote:
        >[color=green]
        > > What are you actually trying to display?
        > >
        > > There is no "SERVER" node in your XML snippet. Even if you change to access
        > > "SERVERS" there is no InnerText on any of those entries. (Just attributes).
        > > Also, why don't you use the .NET Framework instead of the COM XML DOM?
        > >
        > > "Ian" wrote:
        > >[color=darkred]
        > > > Hi
        > > >
        > > > I'm pretty new at this so please don't laugh too hard. I'm trying to load
        > > > an xml document using VB.NET and having a hard time. My code doesn't crash
        > > > but it doesn't work either, the first msgbox returns 0.
        > > >
        > > > Help (please!)
        > > >
        > > >
        > > > Below is my document and snippet of code
        > > >
        > > > <?xml version="1.0" encoding="utf-8"?>
        > > > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
        > > > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
        > > > </TABLEINFORMATIO N>
        > > > <SERVERS hostname="sta0w p02" database="vivid care">
        > > > </SERVERS>
        > > > <SERVERS hostname="rdy1c n02" database="vivid care">
        > > > </SERVERS>
        > > > </CAREKEEPER>
        > > >
        > > > CODE:
        > > >
        > > > Try
        > > > xml_doc.Load(". ./config.xml")
        > > > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
        > > >
        > > > MsgBox(xml_node list.Count())
        > > >
        > > > For Each xml_node In xml_nodelist
        > > > server = xml_node.Item(" SERVER").InnerT ext()
        > > > MsgBox(server)
        > > > Next
        > > >
        > > > Catch ex As Exception
        > > > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & " "
        > > > & ex.Source)
        > > > End Try[/color][/color][/color]

        Comment

        • Abubakar

          #5
          Re: Reading XML File

          Hi,
          what you need is a "namespace manager" to deal with the namespace that is in
          your xml doc. If you add the following code:

          Dim xreader as XmlTextReader = new XmlTextReader(" ../config.xml")
          Dim xns XmlNamespaceMan ager = new new XmlNamespaceMan ager(xreader.Na meTable)
          xns.AddNamespac e("abc", "http://tempuri.org/config1.xsd")

          ...... and a little modification to your xpath query:
          xml_nodelist = xml_doc.SelectN odes("abc:CAREK EEPER",xns)
          gets the node successfully.

          Note: I sort of hard coded the "abc" and the uri, I think u could write it
          in a generic way. Also the xpath is case-sensitive, so be careful while
          writing node names in xpath queries.

          Second solution:
          Just remove the xmlns attribute from the CAREKEEPER node in your xml file
          and the code you write should work without the "/" before CAREKEEPER in
          xpath query.

          I hope this helps.

          Ab.
          Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.


          "Ian" <Ian@discussion s.microsoft.com > wrote in message
          news:7CDBCD18-50AE-4277-93D7-4DB49B386DF1@mi crosoft.com...[color=blue]
          > Hi
          >
          > I'm pretty new at this so please don't laugh too hard. I'm trying to load
          > an xml document using VB.NET and having a hard time. My code doesn't[/color]
          crash[color=blue]
          > but it doesn't work either, the first msgbox returns 0.
          >
          > Help (please!)
          >
          >
          > Below is my document and snippet of code
          >
          > <?xml version="1.0" encoding="utf-8"?>
          > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
          > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
          > </TABLEINFORMATIO N>
          > <SERVERS hostname="sta0w p02" database="vivid care">
          > </SERVERS>
          > <SERVERS hostname="rdy1c n02" database="vivid care">
          > </SERVERS>
          > </CAREKEEPER>
          >
          > CODE:
          >
          > Try
          > xml_doc.Load(". ./config.xml")
          > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
          >
          > MsgBox(xml_node list.Count())
          >
          > For Each xml_node In xml_nodelist
          > server = xml_node.Item(" SERVER").InnerT ext()
          > MsgBox(server)
          > Next
          >
          > Catch ex As Exception
          > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & "[/color]
          "[color=blue]
          > & ex.Source)
          > End Try[/color]


          Comment

          • Abubakar

            #6
            Re: Reading XML File

            and to access attribute you have to write :
            xml_node.Attrib utes["hostname"];

            Any xml related classes I used r inside System.Xml;

            Ab.
            Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.


            "Abubakar" <abubakarm@gmai l.com> wrote in message
            news:OtXtgQxzFH A.3540@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Hi,
            > what you need is a "namespace manager" to deal with the namespace that is[/color]
            in[color=blue]
            > your xml doc. If you add the following code:
            >
            > Dim xreader as XmlTextReader = new XmlTextReader(" ../config.xml")
            > Dim xns XmlNamespaceMan ager = new new[/color]
            XmlNamespaceMan ager(xreader.Na meTable)[color=blue]
            > xns.AddNamespac e("abc", "http://tempuri.org/config1.xsd")
            >
            > ..... and a little modification to your xpath query:
            > xml_nodelist = xml_doc.SelectN odes("abc:CAREK EEPER",xns)
            > gets the node successfully.
            >
            > Note: I sort of hard coded the "abc" and the uri, I think u could write it
            > in a generic way. Also the xpath is case-sensitive, so be careful while
            > writing node names in xpath queries.
            >
            > Second solution:
            > Just remove the xmlns attribute from the CAREKEEPER node in your xml file
            > and the code you write should work without the "/" before CAREKEEPER in
            > xpath query.
            >
            > I hope this helps.
            >
            > Ab.
            > http://joehacker.blogspot.com
            >
            > "Ian" <Ian@discussion s.microsoft.com > wrote in message
            > news:7CDBCD18-50AE-4277-93D7-4DB49B386DF1@mi crosoft.com...[color=green]
            > > Hi
            > >
            > > I'm pretty new at this so please don't laugh too hard. I'm trying to[/color][/color]
            load[color=blue][color=green]
            > > an xml document using VB.NET and having a hard time. My code doesn't[/color]
            > crash[color=green]
            > > but it doesn't work either, the first msgbox returns 0.
            > >
            > > Help (please!)
            > >
            > >
            > > Below is my document and snippet of code
            > >
            > > <?xml version="1.0" encoding="utf-8"?>
            > > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
            > > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
            > > </TABLEINFORMATIO N>
            > > <SERVERS hostname="sta0w p02" database="vivid care">
            > > </SERVERS>
            > > <SERVERS hostname="rdy1c n02" database="vivid care">
            > > </SERVERS>
            > > </CAREKEEPER>
            > >
            > > CODE:
            > >
            > > Try
            > > xml_doc.Load(". ./config.xml")
            > > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
            > >
            > > MsgBox(xml_node list.Count())
            > >
            > > For Each xml_node In xml_nodelist
            > > server = xml_node.Item(" SERVER").InnerT ext()
            > > MsgBox(server)
            > > Next
            > >
            > > Catch ex As Exception
            > > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message &[/color][/color]
            "[color=blue]
            > "[color=green]
            > > & ex.Source)
            > > End Try[/color]
            >
            >[/color]


            Comment

            • Markus Bauer

              #7
              Re: Reading XML File

              Hi,

              did you try:

              xml_nodelist = xml_doc.SelectN odes("/CAREKEEPER")

              Perhaps it is a case sensitive issue?

              Markus

              Ian wrote:[color=blue]
              > Hi
              >
              > I'm pretty new at this so please don't laugh too hard. I'm trying to load
              > an xml document using VB.NET and having a hard time. My code doesn't crash
              > but it doesn't work either, the first msgbox returns 0.
              >
              > Help (please!)
              >
              >
              > Below is my document and snippet of code
              >
              > <?xml version="1.0" encoding="utf-8"?>
              > <CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
              > <TABLEINFORMATI ON TABLE="workers" ssn="ssn" emplid="userinp ut1">
              > </TABLEINFORMATIO N>
              > <SERVERS hostname="sta0w p02" database="vivid care">
              > </SERVERS>
              > <SERVERS hostname="rdy1c n02" database="vivid care">
              > </SERVERS>
              > </CAREKEEPER>
              >
              > CODE:
              >
              > Try
              > xml_doc.Load(". ./config.xml")
              > xml_nodelist = xml_doc.SelectN odes("/carekeeper")
              >
              > MsgBox(xml_node list.Count())
              >
              > For Each xml_node In xml_nodelist
              > server = xml_node.Item(" SERVER").InnerT ext()
              > MsgBox(server)
              > Next
              >
              > Catch ex As Exception
              > MsgBox("XMLPARS ER: CONFIGURATION FILE ERROR " & ex.Message & " "
              > & ex.Source)
              > End Try[/color]

              Comment

              Working...