Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

Read XML Documents with VB.Net

Trying to read an XML document with VB.NET
 but what gets returned is a Tagname and the value concatinated.

Whats wrong ?


Public Sub xreadxml()
        Dim xDoc As New XmlDocument
        Dim xNodeList As XmlNodeList
        Dim xNode As XmlNode
        Dim xSubNode As XmlNode
        Dim xSubList As XmlNodeList
        Dim xClient As XmlNode

        Dim sID As String
        Dim sName As String

        xDoc.Load("C:\Projects\XML Reader.Net\WindowsApplication1\WindowsApplication1\xDoc.xml")

        xNodeList = xDoc.SelectNodes("/Config/Clients")
        For Each xNode In xNodeList
            xSubNode = xNode
            sName = xSubNode.ChildNodes.Item(1).InnerText
        Next
    End Sub

Open in new window

xDoc.xml
0
sidwelle
Asked:
sidwelle
  • 4
  • 4
  • 3
2 Solutions
 
silemoneCommented:
this links looks more suitable to your code try...

http://www.codeproject.com/KB/cpp/parsefilecode.aspx
0
 
silemoneCommented:
don't see anything wrong with the code to be quite honest...except doing this is redundant...i would remove it...

 xSubNode = xNode
            sName = xSubNode.ChildNodes.Item(1).InnerText

and just juse

sName = xNode.ChildNodes.Item(1).InnerText
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sidwelleAuthor Commented:
Yes, I have seen the snip, but what I get is all  the innertText appended:
"Client01ABCD" and "Client02EFGH".

I need to walk through each child nodes ?


0
 
silemoneCommented:
i'm trying it out too to see why
0
 
LabelsoftCommented:
You get "Client01ABCD" because you used ".InnerText" while you are on the element "<Client>". Am I making sense to you?

What I am basically saying is that you need to go one child deeper and then ask the inner text. Ok?

Hope I helped.

p.s.: Have you looked at LINQ to XML yet?
0
 
sidwelleAuthor Commented:
Yes, but its the syntax of how to get to the ChildNodes that is stopping me.

'You would think you could do something like the following:
Dim xCliNode As XmlNodeReader
...
For Each xNode in xNodeList
   xCliNode = xNode.ChildNodes  

0
 
sidwelleAuthor Commented:
Ok, here is a working version:

'Can someone tell me why I need:
xNodeList = xNodeList.Item(0).ChildNodes

'Or a select block to find my values, why not just use this:
sName = xNode.ChildNodes.Item("Name").InnerText

Thanks

Public Sub xxreadxml()
        Dim xDoc As New XmlDocument
        Dim xNodeList As XmlNodeList
        Dim xNode As XmlNode

        Dim sID As String
        Dim sName As String
        Dim sInText As String = "", sNodeName As String = ""
        Dim I As Integer, J As Integer

        xDoc.Load("C:\Projects\XML Reader.Net\WindowsApplication1\WindowsApplication1\xDoc.xml")

        xNodeList = xDoc.SelectNodes("/Config/Clients")
        xNodeList = xNodeList.Item(0).ChildNodes

        'For Each xNode In xNodeList
        For I = 0 To xNodeList.Count - 1
            xNode = xNodeList.Item(I)

            For J = 0 To xNode.ChildNodes.Count - 1

                sInText = xNode.ChildNodes.Item(J).InnerText
                sNodeName = xNode.ChildNodes.Item(J).Name

                Select Case sNodeName

                    Case Is = "Name"
                        sName = sInText
                    Case Is = "ID"
                        sID = sInText

                End Select

            Next
        Next
    End Sub

Open in new window

0
 
LabelsoftCommented:
I don't know what you want me to tell you. There are many ways that lead to Rome. In my humble opinion you choose one of the worst ways to work with XML. Have you looked at XPathNavigator and the Iterator? The thing is, your XML file can be nested. But once you work with it in code it's flat. That's the problem.

That is why I recommended LINQ to XML to you, because you can work with nested or flat XML the same way.
0
 
sidwelleAuthor Commented:
I took your advice and looked at LINQ. Being new to VB.Net and trying to keep this app as light as possible, I just thought it would be nice to use the built in XML class that is included.  I only need it to read a config file once at the startup of the app, and then I don't need it anymore.  I am just disappointed at how un-intuitive and hard it was to use.

Thanks.
0
 
LabelsoftCommented:
Same disappointment here man. The build in classes to work with XML are as unintiutive as they get!  Pretty strange...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now