简体   繁体   English

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

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

I have decided to save some application data using XML files. 我决定使用XML文件保存一些应用程序数据。 I am very new to XML files, so bear with me. 我对XML文件非常陌生,请耐心等待。

I have a class like this): 我有这样的课程):

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 sub results in a file like: 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>

I have several questions: 我有几个问题:

  1. What is the purpose of the "root node" in the XML file? XML文件中“根节点”的目的是什么? Here I created a node "config" in order to get my code working, but I don't really understand why it is necessary. 在这里,我创建了一个节点“ config”以使我的代码正常工作,但是我并不真正理解为什么这样做是必要的。

  2. Is WriteStartDocument/WriteEndDocument required? 是否需要WriteStartDocument / WriteEndDocument?

  3. How can I write a sub to read this file back into my application? 如何编写一个子程序以将该文件读回到我的应用程序中? I am particularly having trouble getting the tables out. 我把桌子拿出来特别麻烦。 I am able to get the single elements out using: 我可以使用以下方法删除单个元素:

     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 

    but I don't know how (or if it is possible) to combine this with: 但我不知道如何(或是否可能)将此与:

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

    It seems like the ReadXml may not actually work for what I'm trying to do (reading specific tables) and is meant for simpler single-table XML structures, but I could not confirm that definitively. 似乎ReadXml可能实际上无法满足我想要做的事情(读取特定表),并且是为更简单的单表XML结构而设计的,但我不能确定地确认这一点。

Any help will be greatly appreciated! 任何帮助将不胜感激!

Try this code, .Net serializer does a lot of work for you. 尝试使用此代码,.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

BTW these code patterns are snippets you can easily reach by right clicking and: Clicking Insert Snippet -> Data - LINQ -XML etc... -> XML -> XML Read / XML Write -> from / to class. 顺便说一下,这些代码模式是您可以通过右键单击并轻松获得的代码段:单击插入代码段->数据-LINQ -XML等...-> XML-> XML读取/ XML写入->从/到类。

How to Ignore a property 如何忽略财产

<XmlIgnore>
Public Property inputsTable As DataTable

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

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