如何读取 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15933333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:58:21  来源:igfitidea点击:

How to read an XML File

xmlvb.netxmlreader

提问by Shmewnix

I have a VB.net program. I'm attempting to use XMLReader to read a .xml file. I want to break the XML File up to organize it into different "Sections" In this example "FormTitle"and "ButtonTitle". I would like to grab the <Text>data from FormTitleand display it as the Form "text"and take the <Text>in "ButtonTitle"and have it display in the button text.

我有一个 VB.net 程序。我正在尝试使用 XMLReader 读取 .xml 文件。我想分解 XML 文件以将其组织成不同的“部分”在这个例子"FormTitle""ButtonTitle". 我想从中获取<Text>数据FormTitle并将其显示为表单,"text"然后将其<Text>输入"ButtonTitle"并显示在按钮文本中。

Here is my XML File:

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<FormTitle>
    <Text>Form Test</Text>
</FormTitle>
<ButtonTitle>
    <Text>Button Test</Text>
</ButtonTitle>

Here is my current Code:

这是我目前的代码:

If (IO.File.Exists("C:\testing.xml")) Then

    Dim document As XmlReader = New XmlTextReader("C:\testing.xml")

    While (document.Read())

        Dim type = document.NodeType


        If (type = XmlNodeType.Element) Then

            '
            If (document.Name = "Text") Then
                Me.Text = document.ReadInnerXml.ToString()


            End If



        End If


    End While

Else

    MessageBox.Show("The filename you selected was not found.")
End If

How can bring in the next section (ButtonTitle)With the same name that is in FormTitlewhich is (Text). I would assume I need to reference FormTitleand ButtonTitlein an if then statement?

怎样才能在下一节中引入(ButtonTitle)同名的就是FormTitle其中的(Text)。我会假设我需要在 if then 语句中引用FormTitle和引用ButtonTitle

回答by Display Name is missing

Check out this example. http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

看看这个例子。http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

You should can use:

你应该可以使用:

doc.GetElementsByTagName("FormTitle")

You can then loop through all of the child nodes. http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes.aspx

然后,您可以遍历所有子节点。http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes.aspx

    Dim root As XmlNode = doc.GetElementsByTagName("FormTitle").Item(1)

    'Display the contents of the child nodes. 
    If root.HasChildNodes Then 
        Dim i As Integer 
        For i = 0 To root.ChildNodes.Count - 1
            Console.WriteLine(root.ChildNodes(i).InnerText)
        Next i
    End If 

回答by James

Using XDocument is more efficient for reading Xml and also more readable due to less syntax.

使用 XDocument 读取 Xml 的效率更高,而且由于语法较少,可读性也更高。

You need to add a root to your XML. I called it root, but it can be anything. It just encapsultes all of your XML

您需要在 XML 中添加一个根。我称它为 root,但它可以是任何东西。它只是封装了您的所有 XML

<?xml version="1.0" encoding="utf-8"?>
<root>
<FormTitle>
    <Text>Form Test</Text>
</FormTitle>
<ButtonTitle>
    <Text>Button Test</Text>
</ButtonTitle>
</root>

Here is an example of pulling the "Form Test" from FormTitle

这是从 FormTitle 中提取“表单测试”的示例

    Dim document As XDocument = XDocument.Load("c:\tmp\test.xml")
    Dim title = From t In document.Descendants("FormTitle") Select t.Value

assign text to form

将文本分配给表单

Form1.Text = title.First()