tags:

views:

82

answers:

4
+3  Q: 

Read XML in VB.net

Guys I'm dying here.

I have googled for the last hour or so with no luck (I'd like to think I'm a great googler too!), so here I am.

I have an XML file that I'm using for my programs settings, it looks like so:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <store>
        <number>0323</number>
        <address>address</address>
        <phone>phone</phone>
    </store>

    <emailsettings>
        <emailfrom>emailfrom</emailfrom>
        <emailpass>pass</emailpass>
        <emailsubject>received</emailsubject>
        <smtpserver>smtp.gmail.com</smtpserver>
        <smtpport>587</smtpport>
        <enablessl>true</enablessl>
        <emailbody>package received</emailbody>
    </emailsettings>
    <dbconfig>
        <dbpath>path</dbpath>
    </dbconfig>
</store>

How can I use vb.net to get each element and return a specific value that I want? Per se, I'd like to return <number> (under <store>) in textbox1, and <emailbody> (under <emailsettings>) in textbox2.

Help pleaseeeeee! Thanks :)

+1  A: 

You can use XML Serialization. Create classes that represent your XML structure and use the XML Serialization Class to deserialize the data. Once you have done that you can use the data in your application.

Below is a link to an example from MSDN:

http://msdn.microsoft.com/en-us/library/ms950721.aspx

Achilles
+1. I use this all the time when I have to read/write XML I have under control.
OregonGhost
A: 

Maybe this will help: http://www.bing.com/search?q=xpath

Sounds like you're asking "What is XPath and how do I use it?"

No Refunds No Returns
+1  A: 

Here's a Console app using the VB.Net XML Literal support.

Module Module1

    Sub Main()

        Dim xElem = <config>
                       <store>
                           <number>0323</number>
                           <address>address</address>
                           <phone>phone</phone>
                       </store>

                       <emailsettings>
                           <emailfrom>emailfrom</emailfrom>
                           <emailpass>pass</emailpass>
                           <emailsubject>received</emailsubject>
                           <smtpserver>smtp.gmail.com</smtpserver>
                           <smtpport>587</smtpport>
                           <enablessl>true</enablessl>
                           <emailbody>package received</emailbody>
                       </emailsettings>
                       <dbconfig>
                           <dbpath>path</dbpath>
                       </dbconfig>
                   </config>

        Dim number = xElem.<store>.<number>.Value
        Dim emailbody = xElem.<emailsettings>.<emailbody>.Value

        Console.WriteLine(String.Format("Number={0}", number))
        Console.WriteLine(String.Format("emailbody={0}", emailbody))
        Console.ReadLine()

        '--- What it'd look like if you loaded from a file using XDocument.Load
        Dim xDoc = XDocument.Load(New IO.StringReader(xElem.ToString))
        number = xDoc.Root.<store>.<number>.Value
        emailbody = xDoc.Root.<emailsettings>.<emailbody>.Value

        Console.WriteLine(String.Format("Number={0}", number))
        Console.WriteLine(String.Format("emailbody={0}", emailbody))
        Console.ReadLine()


    End Sub

End Module

The results will be:

Number=0323
emailbody=package received

Number=0323
emailbody=package received
Rick
+1, since I don't understand the downvote...
Heinzi
Thanks for the +1. :-)
Rick
+2  A: 

Ah, a perfect example for showing off the powerful XML features of VB.NET with Framework 3.5:

Sub Main()
    Dim xml = XDocument.Load("config.xml")
    Console.WriteLine("Number: " & xml.<config>.<store>.<number>.Value)
    Console.WriteLine("Body: " & xml.<config>.<emailsettings>.<emailbody>.Value)
End Sub

yields:

Number: 0323
Body: package received
Heinzi
This is a great answer, WAY better than my serialization answer. Your answer is what I was looking for on my question: http://stackoverflow.com/questions/2253900/generics-and-duck-typing-xml-in-net Please add this answer to my question and I will accept.
Achilles