Results 1 to 5 of 5

Thread: Read specific fragment of XML

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2015
    Posts
    29

    Read specific fragment of XML

    Hi all,

    My XML looks like:
    <item>
    <data1>...</data1>
    </item>
    <item>
    <data1>...</data1>
    </item>

    How to read data only for one "item"? (next one is read when button / action is activated).
    I know how to read data only when one item is located at xml file...

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read specific fragment of XML

    Select all nodes of type <item>
    Code:
            Dim xNodeList As Xml.XmlNodeList
            Dim xDoc As Xml.XmlDocument = New Xml.XmlDocument
            xDoc.Load("path\to\xml\file")
            xNodeList = xDoc.SelectNodes("//item")
    
            For Each n As Xml.XmlNode In xNodeList
                MessageBox.Show(n.InnerText)
            Next
    Select first node of type <item>

    Code:
            Dim xDoc As Xml.XmlDocument = New Xml.XmlDocument
            xDoc.Load("path\to\xml\file")
            Dim n As Xml.XmlNode = xDoc.SelectSingleNode("//item")
            MessageBox.Show(n.InnerText)
    Last edited by 4x2y; May 20th, 2015 at 05:15 PM.



  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2015
    Posts
    29

    Re: Read specific fragment of XML

    OK, when we found a specific "item" how to read specified sub-items for this item?
    like:
    <item>
    <Title>Divine Comedy</Title> ---> send it into Label1
    <Author>Dante</Author> ---> send it into Label2
    </item>

    When next item is selected, the data should be updated...

    BTW. The value is read by "n.InnerText". May I use them as a value to manipulate them?

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read specific fragment of XML

    XmlNode has ChildNodes property which you can use


    Code:
    Dim n As Xml.XmlNode = xDoc.SelectSingleNode("//item")
    
    Label1.Text = n.ChildNodes(0).InnerText
    Label2.Text = n.ChildNodes(1).InnerText



  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read specific fragment of XML

    To get node's value using its name
    Code:
    Dim n As Xml.XmlElement= xDoc.SelectSingleNode("//item")
    
    Label1.Text = n.Item("Title").InnerText
    Label2.Text = n.Item("Author").InnerText



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