好的,这是我完全搞砸的问题。我需要通读几行 xml 条目才能找到我需要的东西。“举个例子”——
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
<Person>
<Name>hayyo</Name>
<Email>222@yolo.com</Email>
<Tel>345</Tel>
</Person>
<Person>
<Name>hihi</Name>
<Email>666@yolo.com</Email>
<Tel>665</Tel>
</Person>
</Data>
j*我现在知道如何处理几行 xml 条目。(为我赢) *
Imports System.Xml
Public Class Form1
Dim XmlOut As XmlWriter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With XmlOut
' Start our first person.
.WriteStartElement("Person")
' The person nodes.
.WriteStartElement("Name")
.WriteString(fullName.Text.ToString())
.WriteEndElement()
.WriteStartElement("Email")
.WriteString(address.Text.ToString())
.WriteEndElement()
.WriteStartElement("Tel")
.WriteString(phoneNum.Text.ToString())
.WriteEndElement()
' The end of this person.
.WriteEndElement()
End With
MessageBox.Show("XML file saved.")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Close the XmlTextWriter.
XmlOut.WriteEndDocument()
XmlOut.Close()
MessageBox.Show("XML file ended.")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim settings As New XmlWriterSettings()
settings.Indent = True
' Initialize the XmlWriter.
XmlOut = XmlWriter.Create("MyName.xml", settings)
' Write the Xml declaration.
XmlOut.WriteStartDocument()
' Write a comment.
XmlOut.WriteComment("XML Database.")
' Write the root element.
XmlOut.WriteStartElement("Data")
End Sub
但是我不知道如何显示条目。我只想显示名称。它现在只会显示最后一个条目。
Imports System.Xml
Imports System.IO
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If (Search.Text = "") Then
MessageBox.Show("No file name entered")
Else
If (System.IO.File.Exists(Search.Text.ToString())) Then
Dim document As XmlReader = New XmlTextReader(Search.Text.ToString())
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name = "Name") Then
XmlName.Visible = True
XmlName.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "Email") Then
XmlAddress.Visible = True
XmlAddress.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "Tel") Then
XmlPhone.Visible = True
XmlPhone.Text = document.ReadInnerXml.ToString()
End If
End If
End While
Else
MessageBox.Show("The filename you selected was not found.")
End If
End If
End Sub
End Class
另外,我将如何搜索 xml 文件以找到 hihi,然后在文件中提取他的电话号码并显示它。
我知道这没什么好做的,但我正在尝试找出如何进行小规模搜索,以便稍后在一个将是一个大文件的项目中进行工作。
我刚刚弄清楚了我未来的任务需要什么,所以我正在尝试编辑一个旧文件以在我开始构建我的任务之前使用它。
只要在正确的方向上一点就会很酷。
任何帮助都是好的,甚至是非常先进的东西。这样我就可以把老师的狗屎弄糊涂了。大声笑 * *
我仍在努力,所以我可能会再次更新。我只有 6 个代表,所以我还不能回答我自己的问题。