Results 1 to 32 of 32

Thread: VB.NET: Get the Attribute and Text out from Xml....[Resolved]

Hybrid View

  1. #1

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

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

    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)

    more sugguestions and related articles will be greatly appreciate...

    Thanks
    Last edited by toytoy; Jan 22nd, 2005 at 12:06 AM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    See if this helps:

    http://www.vbforums.com/showthread.p...hreadid=253267

    There are other previous threads on this topic too if you want to try and search the forums.

  3. #3

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

    I have seem the examples..

    Besides being able to select any attribute and Elements to display, how to actually retrieve all the elements and attributes..
    Say i want to display all in various textboxes at one go ...

    Example of the string: (Taken form the thread given)
    Code:
    <chimaera>
    <servers>
    <host ip="172.16.1.30">Chimaera</host>
    </servers>
    <themes value="true" />
    <locationupdate path="C:\1.xml" />
    </chimaera>
    Which mean the various textboxes should contain:
    Code:
    TextBox1.Text =  172.16.1.30
    TextBox2.Text = Chimaera
    TextBox3.Text = true
    TextBox4.Text = (All the contents in 1.xml)
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:03 AM.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Just replace the code parts that use a Msgbox with the same values into Textboxes. To get the value 'Chimaera' you would refer to the text property of the node itself instead of an attribute but the rest should be the same.

  5. #5

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


  6. #6

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

    Say i have these Xml tag..
    Code:
    <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>
    And the method that i use is XmlDocument..

    If the user key in the text in the textbox...how to tell the XmlDocument that the text refer to certain ID..

    And how to display all the attributes and Elements of that ID to all the textbox.....

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:03 AM.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    In the previous example you notice later on I passed a longer string to the SelectNodes method. This is called an XPath or XQuery and you can query XML using this. To find a specific title by id you would use an XPath something like this:

    VB Code:
    1. For Each nod As XmlNode In xdoc.SelectNodes("//Book/Title[@ID=1]") 'select title node with id=1
    2.             'handle different parts of the Title node here
    3.         Next

    http://www.w3.org/TR/xpath

  8. #8

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Just to ask how this MsgBox part can be replace by string...
    Code:
    Dim xdoc As New XmlDocument
    xdoc.Load("..\data.xml")
    
    For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
         MsgBox(nod.Attributes("ip").Value, , "host")
    Next
    
    MsgBox(xdoc.SelectSingleNode("//themes/@value").Value, , "themes")
    MsgBox(xdoc.SelectSingleNode("//locationupdate/@path").Value, , "locationupdate")
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:04 AM.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Dim xdoc As New XmlDocument
    2. xdoc.Load("..\data.xml")
    3.  
    4. For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
    5.     TextBox1.Text &=nod.Attributes("ip").Value
    6. Next
    7.  
    8. TextBox2.Text=xdoc.SelectSingleNode("//themes/@value").Value
    9. TextBox3.Text=xdoc.SelectSingleNode("//locationupdate/@path").Value

    But really you should add some error handling just in case no matching node is found.
    VB Code:
    1. Dim xdoc As New XmlDocument
    2. xdoc.Load("..\data.xml")
    3.  
    4. For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
    5.     If Not nod Is Nothing Then TextBox1.Text &=nod.Attributes("ip").Value
    6. Next
    7.  
    8. Dim nod As XmlNode
    9. node=xdoc.SelectSingleNode("//themes/@value")
    10. If Not nod Is Nothing Then TextBox2.Text=nod.Value
    11. node=xdoc.SelectSingleNode("//locationupdate/@path")
    12. If Not nod Is Nothing Then TextBox3.Text=nod.Value

  10. #10

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

    if you all guys have found any useful threads in this site or any useful articles besides the above link. .....

    feel free to put the link here...

    any help will be appreciated....


  11. #11

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    one more question...

    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...(in blue)

    Example:
    Code:
    <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; Dec 3rd, 2004 at 08:04 AM.

  12. #12

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    For what i know:

    I must use XPath to search through the required ID so that it will return me back the Elements and attributes for that particular ID.

    But when i read through the XPath articles...I feel that it is some sort like using SelectNodes("//Book/Title) to search and return certain nodes of that string....

    It cannot skip certain tag and retrieve other tags. ...Maybe is my concept weak..
    Say I want to display ID = "2" and web =www.titlebook.com...and skip Author tag..

    Example:
    Code:
    <Books>
    <Book>
    <Title ID ="1">
    <Author>Hello</Author>
    <Reference web = "www.reference.com" topic = "In thing">
    </Title>
    </Book>
    <Book>
    <Title ID = "2"> 
    <Author>Thanks</Author>    'skip this part    
    <Reference web = "www.titlebook.com" topic = "Out thing">
    </Title>
    </Book>
    </Books>
    My coding:
    Code:
    Dim a, b As String
    For Each n In xdoc.SelectNodes("//Title")
     a = n.Attributes("ID").Value
    textBox1.Text = a
    Next
    
    For Each n In xdoc.SelectNodes("//Title/Reference")
    b = n.Attributes("web").Value
    textbox2.Text = b
    Next
    Although it can still be retrieve but it retrieve all...but how to retrieve mainly form ID = "2".... instead of retrieve all the two tags using two loops, how to use it in one loop...

    And in the last two thread that Edneeis send....I feel that it cannot be done since the program does not know which ID until the user key in something inside the textbox....what if there is 100 of IDs which i did not know, is that means that i have to create 100 of that loop..
    Code:
    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..

    Lastly, do you have silimar articles with examples using XPath...
    XPath expert, i need your help in clearing my unclear concepts..

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:05 AM.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The @ is because that item is an attribute of a node. In your schema ID is an attribute of the Title node, web and topic are attributes of the Reference node.

    Here is an example to show you the rest of what to do for that schema specifically.

  14. #14

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

    you are really so good..It solve all my problems...

    Just to ask in the Enter the ID part...

    the user must key in an Integer ID first before retrieve certain data..

    Can it be like the user key in a string and that string is relate to the ID.. and then from there it will show all the Elements and attributes from that ID

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What do you mean? You want the user to search by another node or attribute? If so which one?

    You can just the initial xpath to something like this to search for other things:
    VB Code:
    1. 'by author
    2. Dim xpath As String = String.Format("//Book/Title[Author='{0}']", id)
    3.  
    4. 'by reference topic
    5. Dim xpath As String = String.Format("//Book/Title[Reference/@topic='{0}']", id)

    The search is case-sensitive by the way.

  16. #16

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    What i means is that for example:

    when i key in "France" in the textbox...

    That France will point to the <Title ID ="1"> and show all the Element and attribute inside that ID to the textboxes...

    and when i key in "Singapore" , It will show all the Elements and attribute from <Title ID = "2">

    there is nothing to do with the content of xml files....

    Just used any words to point to that particular main ID inside xml file..

    Is it possible?..
    Last edited by toytoy; Sep 30th, 2004 at 11:54 PM.

  17. #17

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Another question related to the above question..

    Say the Xml tag is:
    Code:
    <Books>
    <Book>
    <Title ID ="1">
    <Author>Hello</Author>
    <Reference web = "www.reference.com" topic = "In thing">
    </Title>
    </Book>
    <Book>
    <Title ID = "2">
    <Author>Thanks</Author>
    <Reference web = "www.titlebook.com" topic = "Out thing">
    </Title>
    </Book>
    </Books>
    Say i entered Hello in the textbox....
    how can i retrieve the ID = "1" and
    display the "In thing" in the textbox..

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:05 AM.

  18. #18

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    ok.... i get what you mean...

    the previous thread is solve...

    what about the France and Singapore words which i post eariler
    Last edited by toytoy; Oct 1st, 2004 at 12:39 AM.

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    How is France and Singapore related to that data?

    I don't understand. The data needs to be related or else how would you look it up?

  20. #20

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    that means if that data is not declare in the Xml file, it cannot be create and point to any of the Elements or attribute...

    is that what you mean...

  21. #21
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If its not in the XML document then it can't be reached via the Xpath query. How is it related? Where is it stored?

  22. #22

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    ok....i fully understand what you mean..

    now my concept on XPath is much more clearer...
    I have finally resolve all my doubts..

    Thanks Edneeis..
    Last edited by toytoy; Oct 1st, 2004 at 01:32 AM.

  23. #23

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

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

    Say i have these Xml tag
    Code:
    <Books>
          <Title section ="1">
              <Reference web = "www.any.com" topic = "1">
          </Title>
          <Title section ="1">
              <Reference web = "www.idea.com" topic = "2">
          </Title>
          <Title section ="2">
              <Reference web = "www.howTo.com" topic = "3">
          </Title>
          <Title section ="3">
              <Reference web = "www.do.com" topic = "4">
          </Title>
    </Books>
    I want to extract all the web and topic attribute only in Title section "1"...

    Thanks
    Last edited by toytoy; Jan 6th, 2005 at 09:14 AM.

  24. #24
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

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

    Did you check out the example code I posted in the zip file?

    It shows you how to do that. The only difference would be referring to the topic attribute exactly the way the example does the web attribute.

  25. #25

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

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

    yeah....cannot..

    Once i selected ID "1" which is the same with two title data..

    it display only the first data of web and topics since two have the same ID..

    Say how to extract all web and topic involve same and different ID into the combo box ..

    Thanks..

  26. #26
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

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

    Then instead of calling SelectSingleNode use SelectNodes and loop through the results.

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