Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read XML in VB.net

Tags:

xml

vb.net

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>
</config>

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 :)

like image 495
Josh streit Avatar asked Feb 15 '10 16:02

Josh streit


People also ask

How to use XML in VB net?

Loads the XML document from the specified TextReader. Loads the XML document from the specified XmlReader. Loads the XML document from the specified string. Adds the specified node to the beginning of the list of child nodes for this node.

How do I read XML files?

View an XML file in a browser If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".

How do I view an XML file in Visual Studio?

On the File menu, point to New, and click File. Select XML File in the Templates pane and click Open. A new file is opened in the editor.

What is .NET XML?

XML stands for Extensible Markup Language. It is a text-based markup language derived from Standard Generalized Markup Language (SGML). XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are used to display the data.


1 Answers

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
like image 141
Heinzi Avatar answered Sep 19 '22 08:09

Heinzi