繁体   English   中英

VB.NET:从XML文件读取多个属性和表

[英]VB.NET: Read Multiple Properties and Tables from XML File

我决定使用XML文件保存一些应用程序数据。 我对XML文件非常陌生,请耐心等待。

我有这样的课程):

Public Class config
    Public Property name As String
    Public Property type As String

    Public Property inputsTable As DataTable
    Public Property outputsTable As DataTable

    Public Sub WriteXML(filePath As String)
        Using writer As XmlWriter = XmlWriter.Create(filePath)
            writer.WriteStartDocument()
            writer.WriteStartElement("config")

            writer.WriteElementString("name", Me.name)
            writer.WriteElementString("type", Me.type)

            Me.inputsTable.WriteXml(writer)
            Me.outputstable.WriteXml(writer)

            writer.WriteEndElement()
            writer.WriteEndDocument()
        End Using
    End Sub
End Class

WriteXML子生成的文件类似:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <name>testConfigName</name>
    <type>testConfigType</type>
    <DocumentElement>
        <inputs>
            <inputName>testInputName1</inputName>
            <inputValue>testInputValue1</inputValue>
        </inputs>
        <inputs>
            <inputName>testInputName2</inputName>
            <inputValue>testInputValue2</inputValue>
        </inputs>
    </DocumentElement>
    <DocumentElement>
        <outputs>
            <outputName>testOutputName1</outputName>
            <outputValue>testOutputValue1</outputValue>
        </outputs>
    </DocumentElement>
</config>

我有几个问题:

  1. XML文件中“根节点”的目的是什么? 在这里,我创建了一个节点“ config”以使我的代码正常工作,但是我并不真正理解为什么这样做是必要的。

  2. 是否需要WriteStartDocument / WriteEndDocument?

  3. 如何编写一个子程序以将该文件读回到我的应用程序中? 我把桌子拿出来特别麻烦。 我可以使用以下方法删除单个元素:

     Using reader As XmlReader = XmlReader.Create(filePath) While reader.Read() Select Case reader.NodeType Case XmlNodeType.Element Select Case reader.Name Case "name" name = reader.ReadElementContentAsString() Case "type" type = reader.ReadElementContentAsString() End Select End Select End While End Using 

    但我不知道如何(或是否可能)将此与:

     inputsTable.ReadXml(reader) outputsTable.ReadXml(reader) 

    似乎ReadXml可能实际上无法满足我想要做的事情(读取特定表),并且是为更简单的单表XML结构而设计的,但我不能确定地确认这一点。

任何帮助将不胜感激!

尝试使用此代码,.Net序列化程序可以为您完成很多工作。

Public Class config
    Public Property name As String
    Public Property type As String

    Public Property inputsTable As DataTable
    Public Property outputsTable As DataTable

    Public Sub WriteXML(filePath As String)

        Dim writer As New XmlSerializer(GetType(config))
        Dim file As New StreamWriter(filePath)
        writer.Serialize(file, Me)
        file.Close()
    End Sub

    Public Shared Function ReadXML(filePath As String) As config

        Dim reader = New XmlSerializer(GetType(config))
        Dim file = New StreamReader(filePath)
        Dim fileData = CType(reader.Deserialize(file), config)

        Return fileData

    End Function

End Class

顺便说一下,这些代码模式是您可以通过右键单击并轻松获得的代码段:单击插入代码段->数据-LINQ -XML等...-> XML-> XML读取/ XML写入->从/到类。

如何忽略财产

<XmlIgnore>
Public Property inputsTable As DataTable

暂无
暂无

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

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