CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2004
    Posts
    67

    VB.NET: Get the Attribute and Text out from Xml string....[Unsolved]

    Say i receive this string from the server:
    s = "<Main Type="Personal">Title</Main>"

    How to extract the attributes = "Personal" from Main tag
    and the Text ="Title" using LoadXml(s)

    Thanks
    Last edited by toytoy; October 26th, 2004 at 07:05 AM.

  2. #2
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: VB.NET: Get the Attribute and Text out from Xml string

    Give this a try:
    Code:
            ' Load up the XML file
            Dim xmlDoc As New XmlDocument
            xmlDoc.LoadXml("<Main Type='Personal'>Title</Main>")
    
            Dim n As XmlNode
            ' Scroll through the nodes
            For Each n In xmlDoc.SelectNodes("Main")
                Debug.WriteLine(n.Attributes("Type").Value)
                Debug.WriteLine(n.InnerText)
            Next
    Hope this helps,

    Nathan.

  3. #3
    Join Date
    Aug 2004
    Posts
    67

    Re: VB.NET: Get the Attribute and Text out from Xml string

    Thanks...it works

    How to create a loop to see all InnerText and Attributes...
    What if i want to select only either all the attributes or InnerText to view....

    Example:
    LoadXml("<Main Type='Personal'><Time Hour="23" Min = "50"> Appointmnet</Time><Time Hour="10" Min = "40">Booking</Time></Main>)"

    Any articles or examples to go about it...

    Thanks
    Last edited by toytoy; September 27th, 2004 at 10:23 PM.

  4. #4
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: VB.NET: Get the Attribute and Text out from Xml string

    Well, the code I sent through does actually loop through all the nodes:

    Code:
            ' Load up the XML file
            Dim xmlDoc As New XmlDocument
            xmlDoc.LoadXml("<root><Main Type='Personal'>Title</Main><Main Type='Relative'>Sub</Main></root>")
    
            Dim n As XmlNode
            ' Scroll through the nodes
            For Each n In xmlDoc.SelectNodes("root/Main")
                Debug.WriteLine(n.Attributes("Type").Value)
                Debug.WriteLine(n.InnerText)
            Next
    Hope this helps,

    Nathan.

  5. #5
    Join Date
    Aug 2004
    Posts
    67

    Smile Re: VB.NET: Get the Attribute and Text out from Xml string

    Sorry..

    As i rewrite the code, you at the same time send me the solution..

    How about the above edit example

    I will try your given examples..

    Thanks a lot
    Last edited by toytoy; September 27th, 2004 at 09:13 PM.

  6. #6
    Join Date
    Aug 2004
    Posts
    67

    Re: VB.NET: Get the Attribute and Text out from Xml string

    Besides using For Each /Next loop to find every individual nodes, which loop should be use and how to apply if i want only one set of tag...
    ( all attribute and element in ID =2 for the Title tag)

    Example:

    <Book>
    <Title ID ="1"> <Author>Hello</Author>
    <Reference web = "www.reference.com" topic = "In thing">
    </Title>
    <Title ID = "2">
    <Author>Thanks</Author>
    <Reference web = "www.titlebook.com" topic = "Out thing">
    </Title>
    </Book>

    I just want a draft idea of how it should be like...
    Last edited by toytoy; September 30th, 2004 at 03:28 AM.

  7. #7
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: VB.NET: Get the Attribute and Text out from Xml string

    Ahhhh, gotcha.

    OK, firstly, your syntax for the XML file isn't quite correct. It should be more like this:

    <Book>
    <Title ID ="1">
    <Author>Hello</Author>
    <Reference web = "www.reference.com" topic = "In thing" />
    </Title>
    <Title ID = "2">
    <Author>Thanks</Author>
    <Reference web = "www.titlebook.com" topic = "Out thing" />
    </Title>
    </Book>

    You missed out the / on the end of your reference line.

    Now, the code you want is something like this I think:

    Code:
    Dim xmlDoc As New XmlDocument
    xmlDoc.LoadXml("<Book><Title ID ='1'> <Author>Hello</Author><Reference web = 'www.reference.com' topic = 'In thing' /></Title><Title ID = '2'><Author>Thanks</Author><Reference web = 'www.titlebook.com' topic = 'Out thing' /></Title></Book>")
    
    ' This will get the node with an ID = 2 in the Title field
    Dim n As XmlNode = xmlDoc.SelectSingleNode("Book/Title[@ID='2']")
    
    ' From here you can get your data this way
    Debug.WriteLine("Author: " & n.SelectSingleNode("Author").InnerText)
    Debug.WriteLine("Reference web: " & n.SelectSingleNode("Reference").Attributes("web").Value)
    Hope this helps,

    Nathan.

  8. #8
    Join Date
    Aug 2004
    Posts
    67

    Re: VB.NET: Get the Attribute and Text out from Xml string

    what if there is 100 of IDs which i did not know, is that means that i have to create 100 of that loop..

    And i read from some forums website..

    TextBox2.Text=xdoc.SelectSingleNode("//themes/@value").Value
    TextBox3.Text=xdoc.SelectSingleNode("//locationupdate/@path").Value

    What is the @ represent in the above coding... and how it works..

    thanks...
    Last edited by toytoy; September 30th, 2004 at 09:33 PM.

  9. #9
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: VB.NET: Get the Attribute and Text out from Xml string

    OK, not sure what you're after now. I'll try and answer what I think you're asking.

    If you want to loop through all the nodes:
    Code:
    Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title")
    Dim n As XmlNode
    
    For Each n In nl
          Debug.WriteLine("Author: " & n.SelectSingleNode("Author").InnerText)
          Debug.WriteLine("Reference web: " & n.SelectSingleNode("Reference").Attributes("web").Value)
    Next
    If you just want to list all the web attributes for the Reference nodes, you could do something like:
    Code:
    Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title/Reference/@web")
    Dim n As XmlNode
    
    For Each n In nl
          Debug.WriteLine("Reference web: " & n.InnerText)
    Next
    If you know the ID and you want to get all the child nodes and their values and attributes:
    Code:
    Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='2']").ChildNodes
    Dim n As XmlNode
    Dim a As XmlAttribute
    
    For Each n In nl
          Debug.WriteLine(n.Name & " - " & n.InnerText)
          For Each a In n.Attributes
              Debug.WriteLine("  " & a.Name & " - " & a.Value)
          Next
    Next
    If you know the ID you're after and you want to get an attribute:
    Code:
    xmlDoc.SelectSingleNode("/Book/Title[@ID='2']/Reference/@web").Value
    The @ refers to an attribute.

    Does any of that answer your question?

  10. #10
    Join Date
    Aug 2004
    Posts
    67

    Re: VB.NET: Get the Attribute and Text out from Xml string

    no...

    What i means if all the ID is not known....until runtime

    How to you put in in your code....if i retrieve 100 ID..

    that means i have to code

    Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='2']").ChildNodes
    'do something------------------------------------------------
    Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='3']").ChildNodes
    'do something-----------------------------------------------
    -----------------------------------------------------------------
    -----------------------------------------------------------------
    Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='100']").ChildNodes
    'do something------------------------------------------------

    is it a waste of time...

  11. #11
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: VB.NET: Get the Attribute and Text out from Xml string

    No, if you don't know the ID's and you want to go through all the children:
    Code:
    Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title")
    Dim n, pn As XmlNode
    Dim a As XmlAttribute
    
    For Each pn In nl
         Debug.WriteLine("Title ID: " & pn.Attributes("ID").Value)
         For Each n In pn.ChildNodes
             Debug.WriteLine(n.Name & " - " & n.InnerText)
             For Each a In n.Attributes
                 Debug.WriteLine("  " & a.Name & " - " & a.Value)
             Next
        Next
    Next
    Is that what you're after?

  12. #12
    Join Date
    Aug 2004
    Posts
    67

    Re: VB.NET: Get the Attribute and Text out from Xml

    Say I have this Xml taken as an example:

    Code:
    <Settings>
      <Connection>
             <UserID>sa</UserID>
             <DataSource>Server</DataSource>
             <InitialCatalog>Database</InitialCatalog>
      </Connection>
    </Settings>
    To get the Xml text is:
    Code:
      Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        
        m_xmld = New XmlDocument()
    
        m_xmld.Load("Settings.xml")
    
        m_nodelist = m_xmld.SelectNodes("/Settings/Connection")
        
        For Each m_node In m_nodelist
           Dim UserID = m_node.ChildNodes.Item(0).InnerText
           Dim DataSource = m_node.ChildNodes.Item(1).InnerText
           Dim InitialCatalog = m_node.ChildNodes.Item(2).InnerText
        Next
    How about if i add in one time tag...
    Code:
    <Settings>
      <Connection>
             <UserID>sa</UserID>
             <DataSource>Server</DataSource>
             <InitialCatalog>Database</InitialCatalog>
                 <Register>
                    <Time hour="23" min=40">
                    <Date>22/10/78</Date>
                 </Register>
      </Connection>
      <Connection>
             <UserID>sa</UserID>
             <DataSource>Server</DataSource>
             <InitialCatalog>Database</InitialCatalog>
                 <Register>
                    <Time hour="23" min=40">
                    <Date>22/10/78</Date>
                 </Register>
      </Connection>
    </Settings>
    How do i use the above node.ChildNodes method to get the "23" or "40" out from the Xml tag....

    I need to loop through all the nodes...and I am using XmlDocument...

    Any suggestion or link will be appreciated...

    Thanks
    Last edited by toytoy; October 25th, 2004 at 11:36 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured