gpt4 book ai didi

xml - 存储 XML 数据的优雅解决方案

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:08 25 4
gpt4 key购买 nike

我已经让我的应用程序从我的 xml 文档中读取一些值,但是我不确定我将如何存储它们,因为目前文件中的每个元素总共有 6 条信息。

XML 范例

<?xml version="1.0" encoding="utf-8"?>
<App>
<Name>First Application </Name>
<FileName>1.exe</FileName>
<FilePath>C:\</FilePath>
<Third_Parameter>etc</Third_Parameter>
<Forth_Parameter>etc</Forth_Parameter>
<Name>Application 2</Name>
<FilePath></FilePath>
<Third_Parameter>etc</Third_Parameter>
<Forth_Parameter>etc</Forth_Parameter>
</App>

我正在考虑一个具有唯一 ID 的数组,因为无论如何我已经为每个应用程序创建了一个,但我不知道如何动态创建一个具有另一个变量名称的数组。我查看了使用字典,但是我有两个以上的变量,不知道如何使用它。

基本上,我需要一种方法来为可能无限量的应用程序存储所有这些信息,而无需使用数据库。

最佳答案

将 XML 结构更改为树状结构会很有用,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Apps>
<App Name="First Application">
<FileName>1.exe</FileName>
<FilePath>C:\</FilePath>
<Third_Parameter>etc</Third_Parameter>
<Forth_Parameter>etc</Forth_Parameter>
</App>
<App Name="Application 2">
<FilePath></FilePath>
<Third_Parameter>etc</Third_Parameter>
<Forth_Parameter>etc</Forth_Parameter>
</App>
</Apps>

那么你可以上这个课:

Class App
Public FileName As String
Public FilePath As String
Public Third_Parameter As String
Public Forth_Parameter As String
Public AppName As String
End Class

还有一个 (String,App) 的字典 - 假设您将按名称进行索引。

您可以像这样填充它(其中 xml 类型为 XDocument):

Dim dict As New Dictionary(Of String, App)
For Each elem As XElement In xml.Elements()
Dim app As New App 'each element would be App
With app
.AppName = elem.Attribute("Name").Value
.FileName = elem.Element("FileName").Value
.FilePath = elem.Element("FilePath").Value
.Third_Parameter = elem.Element("Third_Parameter").Value
.Forth_Parameter = elem.Element("Forth_Parameter").Value
End With
dict.Add(app.AppName, app)
Next

如果您想要更少的代码,请考虑研究 XML 序列化。我通过谷歌搜索找到的一些示例:

关于xml - 存储 XML 数据的优雅解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com