简体   繁体   English

在VB.net中读取XML

[英]Read XML in 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. 我用google搜索了最后一个小时左右没有运气(我想我也是一个伟大的Google员工!),所以我在这里。

I have an XML file that I'm using for my programs settings, it looks like so: 我有一个XML文件,我用于我的程序设置,它看起来像这样:

<?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? 我如何使用vb.net获取每个元素并返回我想要的特定值? Per se, I'd like to return <number> (under <store> ) in textbox1, and <emailbody> (under <emailsettings> ) in textbox2. 本身,我想在textbox1中返回<number> (在<store> ),在textbox2中返回<emailbody> (在<emailsettings> )。

Help pleaseeeeee! 帮助pleaseeeeee! Thanks :) 谢谢 :)

Ah, a perfect example for showing off the powerful XML features of VB.NET with Framework 3.5: 啊,一个用Framework 3.5展示VB.NET强大的XML特性的完美例子:

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

Here's a Console app using the VB.Net XML Literal support. 这是一个使用VB.Net XML Literal支持的Console应用程序。

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 

You can use XML Serialization. 您可以使用XML序列化。 Create classes that represent your XML structure and use the XML Serialization Class to deserialize the data. 创建表示XML结构的类,并使用XML序列化类对数据进行反序列化。 Once you have done that you can use the data in your application. 完成后,您可以使用应用程序中的数据。

Below is a link to an example from MSDN: 以下是MSDN示例的链接:

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

If you are familiar with datasets and datatables, this is an easy way to do it: 如果您熟悉数据集和数据表,这是一种简单的方法:

Dim DS As New DataSet
DS.ReadXml("Data.xml")

DataSets can read any XML. DataSet可以读取任何XML。 I even use this to call rest-enabled XML services. 我甚至用它来调用支持rest的XML服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM