Results 1 to 6 of 6

Thread: VB.NET: Getting Attributes and Text out from Xml 2....[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Thumbs up VB.NET: Getting Attributes and Text out from Xml 2....[Resolved]

    Say I have this Xml taken from one of the threads as 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>
             <Time hour="23" min="40">
      </Connection>
      <Connection>
             <UserID>sa</UserID>
             <DataSource>Server</DataSource>
             <InitialCatalog>Database</InitialCatalog>
             <Time hour="23" min="40">
      </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; Dec 3rd, 2004 at 07:51 AM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Should be

    m_node.ChildNodes.Item(3).Attributes(0).Value for hour

    change to Attributes(1) for min
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    What about the below Xml..
    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 to loop using the same nod.ChildNode method to extract the hour, min and DataSource...

    Thanks..
    Last edited by toytoy; Dec 3rd, 2004 at 07:52 AM.

  4. #4

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Cander...the code give me an error:

    Code:
    Additional information: The index being passed in is out of range.
    My coding:
    Code:
     Dim xdoc As New XmlDocument
     xdoc.Load("Setting.xml")
     Dim nodlist As XmlNodeList
     Dim nod As XmlNode
         
     nodlist = xdoc.SelectNodes("//Setting/Connection")
    
     For Each nod In nodlist
     Dim hour = nod.ChildNodes.Item(3).Attributes(0).Value
     listbox.Items.Add(hour)
     Dim min = nod.ChildNodes.Item(3).Attributes(1).Value
     listbox.Items.Add(min)
     Dim dataSource = nod.ChildNodes.Item(1).InnerText
     listbox.Items.Add(dataSource)
     Next
    Anyone can help...

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:52 AM.

  5. #5

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    finally solve it...
    Code:
    <Settings>
      <Connection>
             <UserID>ok</UserID>
             <DataSource>Server</DataSource>
             <InitialCatalog>Database</InitialCatalog>
                 <Register>
                    <Time hour="12" min="34">
                    <Date>12/6/04</Date>
                 </Register>
      </Connection>
      <Connection>
             <UserID>thank</UserID>
             <DataSource>Client</DataSource>
             <InitialCatalog>Database</InitialCatalog>
                 <Register>
                    <Time hour="56" min="78">
                    <Date>22/10/05</Date>
                 </Register>
      </Connection>
    </Settings>
    this is the coding
    Code:
    Dim m_xmld As XmlDocument
    Dim m_nodelist As XmlNodeList
    Dim m_node As XmlNode
    
    m_xmld = New XmlDocument
    m_xmld.Load("Setting.xml")
    
    m_nodelist = m_xmld.SelectNodes("/Settings/Connection")
    
    For Each m_node In m_nodelist
        Dim objSubs As Xml.XmlNodeList = m_node.ChildNodes
        Dim m_nodeSubs As XmlNode
        MessageBox.Show(m_node("DataSource").InnerText) 
    
          For Each m_nodeSubs In objSubs
              Dim objAttr As Xml.XmlNodeList = m_nodeSubs.ChildNodes
              Dim m_nodeAttr As XmlNode
    
                 For Each m_nodeAttr In objAttr
                     Select Case m_nodeAttr.Name
                           Case "time"
                                MessageBox.Show(m_nodeAttr.Attributes("hour").InnerText) 
                                MessageBox.Show(m_nodeAttr.Attributes("min").InnerText) 
                     End Select
                Next
          Next
     Next
    Last edited by toytoy; Dec 3rd, 2004 at 07:52 AM.

  6. #6

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Just to share with you guys..

    actually the ChildNodes method also works..

    This code select all the tag...
    DataSource alone can be selected also....
    just delete UserID and InitialCatalog parts will do..

    The coding:
    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
       MesageBox.Show(UserID)
       Dim DataSource = m_node.ChildNodes.Item(1).InnerText
       MesageBox.Show(DataSource)
       Dim InitialCatalog = m_node.ChildNodes.Item(2).InnerText
       MesageBox.Show(InitialCatalog)
       Dim registerNode As XmlNode = m_node.ChildNodes.Item(2).ChildNodes.Item(0)
       Dim hour = registerNode.Attributes(0).InnerText
       Dim min = registerNode.Attributes(1).InnerText
       MesageBox.Show(hour)
       MesageBox.Show(min)
    Next
    Last edited by toytoy; Dec 3rd, 2004 at 07:53 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