在启动时通过 VB.Net 从 XML 文件写入和读取数据 [英] Write and read data from XML file through VB.Net at launch

查看:36
本文介绍了在启动时通过 VB.Net 从 XML 文件写入和读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的一年半里,我开始在 VB.Net 中做一些应用程序,尽管我做了很多研究来寻找答案,但对我来说没有任何效果,所以我在这里提出我的问题.

I started doing some applications in VB.Net in the past year and a half and despite all my research for an answer, nothing has worked fine enough for me, so here I am, asking my question.

我有一个应用程序,它允许用户将以下数据保存在 XML 文件中:

I have an application which allows the user to save the following data in an XML file:

  1. 上面保存数据的自定义名称(在名为txtName的文本框中输入)
  2. 电子邮件(在名为 txtEmail 的文本框中输入)
  3. 密码(在名为 txtPassword 的文本框中输入)
  1. Custom name for the saved data above (inputted in a text box called txtName)
  2. Email (inputted in a text box called txtEmail)
  3. Password (inputted in a text box called txtPassword)

XML 文件(名称为:appData.xml - 有关可执行文件的目录以及 xml 文件的目录,请参见下文)类似于下面显示的模板.该 xml 文件总共存储 10 个密码,每个密码都被赋予一个 ID 号.我认为我需要指定一个 ID 号(从 0110)才能在我的应用程序代码中引用它.如果有更简单的方法来确定保存数据的存储位置,请随时修改我的 xml 文件.

The XML file (with the name: appData.xml - see below for directory of executable file along with directory of xml file) looks like this template shown below. The xml file stores a total of 10 passwords and each password is being attributed an ID number. I have thought that I would need to attribute an ID number (from 01 to 10) to refer it in the code of my application. If there is an easier way to identify where to store the saved data, feel free to modify my xml file.

<?xml version="1.0" encoding="UTF-8"?>
<savedData>
    <savedPassword id="01">
        <name>Stored Name</name>
        <email>storedname@email.com<email>
        <password>storedPassword</password>
    </savedPassword>
    <savedPassword id="02">
        <name>Stored Name</name>
        <email>storedemail@email.com<email>
        <password>storedPassword</password>
    </savedPassword>
    <-- Other data modules for ids 03 to 09 --!>
    <savedPassword id="10">
        <name />
        <email />
        <password />
    </savedPassword>
</savedData>

XML 位于以下文件夹中:C:\VelocityDK Codes\Password Manager,应用程序的可执行文件位于以下文件夹中:[USERPROFILE]\Documents\VelocityDK Codes([USERPROFILE]"是我的硬盘驱动器上我的文档文件夹的部分路径).由于我对 VB.Net(windows 表单)中的 XML 处理很陌生,我不知道是否需要表单的名称来从 xml 中检索数据 - 但如果是,这里是:frmMain.

The XML is located in the following folder: C:\VelocityDK Codes\Password Manager and the application's executable is located in the following folder: [USERPROFILE]\Documents\VelocityDK Codes ("[USERPROFILE]" being the partial path to my documents folder on my Hard Drive). As I am quite new to XML handling in VB.Net (windows forms), I have no idea if the name of the form is needed to retrieve the data from the xml - but if it is, here it is: frmMain.

遗憾的是,我没有足够的经验来存储来自外部数据文件的数据,并在加载可执行文件的情况下从位于与应用程序文件完全不同的目录中的文件中提取数据,因此我实际上没有任何经验提供的代码...因此,我想要做的是以下内容:我需要在每次启动时自动从我的 XML 文件中检索数据.

Sadly, I do not have enough experience in storing data from external data files and pulling that data from a file located in a completely different directory than the application file withing the load of the executable files, so I do not actually have any code to provide... Therefore, what I want to do is the following: I need to retrieve the data from my XML file at each launch automatically.

推荐答案

使用 XML 序列化.IMO 在大多数情况下这优于使用 XDocuments.您将拥有一个带有子代的对象,这些对象代表您的 xml 文件内容.您可以迭代子项,并且所有内容都是强类型的.

Use XML Serialization. IMO this is superior to using XDocuments in most cases. You will have an object with children which represent your xml file contents. You can iterate of the children, and everything is strongly-typed.

首先创建 VB.NET 类来表示您的数据

Start by creating VB.NET classes to represent your data

Option Strict On

Imports System.IO
Imports System.Xml.Serialization

<XmlRoot("savedData")>
Public Class SavedData
    <XmlElement("savedPassword")>
    Public Property SavedPasswords As List(Of SavedPassword)
End Class

Public Class SavedPassword
    <XmlAttribute("id")>
    Public Property ID As Byte
    <XmlElement("name")>
    Public Property Name As String
    <XmlElement("email")>
    Public Property Email As String
    <XmlElement("password")>
    Public Property Password As String
End Class

一旦你有了它,将文件读入你的类就非常简单了.使用以下代码

Once you have that, reading the file into your classes is pretty simple. Use the following code

Dim filename = "filename.xml"
Dim data As SavedData
Dim serializer As New XmlSerializer(GetType(SavedData))
Using sr = New StreamReader(filename)
    data = CType(serializer.Deserialize(sr), SavedData)
End Using
For Each sp In data.SavedPasswords
    Console.WriteLine($"id: '{sp.ID}', name: '{sp.Name}', email: '{sp.Email}', password: '{sp.Password}'")
Next

您唯一需要做的就是设置正确的文件名而不是Dim filename = "filename.xml".如果路径是固定的,您可以简单地对其进行硬编码

The only thing you need to do is to set the correct filename instead of Dim filename = "filename.xml". If the path is fixed, you can simply hard-code it

Dim filename = "C:\VelocityDK Codes\Password Manager\filename.xml"

如果路径可以变化,例如基于 Environment.SpecialFolder Enum 您可以将其合并到作业中:

If the path can vary such as being based on the Environment.SpecialFolder Enum you can incorporate it into the assignment:

执行程序集不必与 xml 文件位于同一文件夹中.

The executing assembly needn't be in the same folder as the xml file.

这篇关于在启动时通过 VB.Net 从 XML 文件写入和读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆