Results 1 to 5 of 5

Thread: reading xml

  1. #1

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    reading xml

    Hello,
    I have an xml document similar to the what's below. I'm wondering how I can read the document and all of its child nodes. I want to only read the parent node called MYTYPES and disregard MYGRAPH. The code I am using reads the document but only prints out the parent node (Type1 and Type 2, in this example). How do i get all the child nodes, and their children. For example, i would like to print out:

    Type 1: Clarion, 10. Pittsburgh, 20
    Type 2: Trention, 10. Harrisburgh, 20

    Code:
                 Try
                    Dim ds As New DataSet
                    ds.ReadXml(xmlFilePath & "myFile.xml")
                    For Each r As DataRow In ds.Tables(0).Rows
                        Debug.Print(r.Item(0).ToString)
                    Next
                Catch ex As Exception
                    Throw
                End Try
    Code:
    <?xml version="1.0" standalone="yes"?>
    <MYPARENT>
      <MYTYPES>
        <MYTYPE>Type 1</MYTYPE>
        <MYLOCATIONS>
          <LocationName>Clarion</LocationName>
          <Age>10</Age>
        </MYLOCATIONS>
        <MYLOCATIONS>
          <LocationName>Pittsburgh</LocationName>
          <Age>20</Age>
        </MYLOCATIONS>
      </MYTYPES>
    
      <MYTYPES>
        <MYTYPE>Type 2</MYTYPE>
        <MYLOCATIONS>
          <LocationName>Trenton</LocationName>
          <Age>10</Age>
        </MYLOCATIONS>
        <MYLOCATIONS>
          <LocationName>Harisburgh</LocationName>
          <Age>20</Age>
        </MYLOCATIONS>
      </MYTYPES>
     
      <MYGRAPH>
        <GRAPHNAME>NAME1</GRAPHNAME>
        <TOTAL>1</TOTAL>
      </MYGRAPH>
      <MYGRAPH>
        <GRAPHNAME>NAME2</GRAPHNAME>
        <TOTAL>2</TOTAL>
      </MYGRAPH>
    
    </MYPARENT>
    if i was able to help, rate my post!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: reading xml

    If you're using VS2008 or later with .Net 3.5 then you can do this:-
    vbnet Code:
    1. '
    2.         Dim doc As XDocument = XDocument.Parse(XMLString)
    3.  
    4.         Dim types = doc...<MYTYPES>
    The types variable would have a reference to an IEnumerable<T> object of XElements.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: reading xml

    sorry, i should have specified. for this project, I am using Visual Studio 2005 and .net 2.0
    if i was able to help, rate my post!

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: reading xml

    Then use the XmlDocument class instead:-
    vbnet Code:
    1. '
    2.         Dim doc As New XmlDocument
    3.         doc.LoadXml(XMLString)
    4.  
    5.         Dim types = doc.GetElementsByTagName("MYTYPES")
    types would be a reference to an XmlNodeList object. Also be sure to import the System.Xml namespace.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: reading xml

    thanks, ill give that a try
    if i was able to help, rate my post!

Tags for this Thread

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