VB.NET 读取XML中的某一子节点中的信息解决方法
VB.NET 读取XML中的某一子节点中的信息
写了如下代码,创建了一个XML
创建的XML如下:↓
现在我想分别得到FirstName、LastName、EmployeeId中的数据,具体怎么读取
能直接用XmlTextReader吗?越具体越好!谢谢
我是用这个XML保存我的程序的数据,用户关闭程序后再打开程序做到读取XML中信息
新人,多多关照!
------解决方案--------------------
这个xml的文档的格式有点问题,没有root.
应该是
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Compnay>
<Employees>
<Employee>
<FirstName>Albert</FirstName>
<LastName>Anders</LastName>
<EmployeeId>11111</EmployeeId>
</Employee>
<Employee>
<FirstName>Betty</FirstName>
<LastName>Beach</LastName>
写了如下代码,创建了一个XML
- VB.NET code
Imports System.Xml Imports System.IO Public Class Form1 Private Sub btnGo_Click() Handles btnGo.Click Using memory_stream As New MemoryStream() Dim xml_text_writer As New XmlTextWriter(memory_stream, System.Text.Encoding.UTF8) ' Use indentation to make the result look nice. xml_text_writer.Formatting = Formatting.Indented xml_text_writer.Indentation = 4 ' Write the XML declaration. xml_text_writer.WriteStartDocument(True) ' Start the Employees node. xml_text_writer.WriteStartElement("Employees") ' Write some Employee elements. MakeEmployee(xml_text_writer, "Albert", "Anders", 11111) MakeEmployee(xml_text_writer, "Betty", "Beach", 22222) MakeEmployee(xml_text_writer, "Chuck", "Cinder", 33333) ' End the Employees node. xml_text_writer.WriteEndElement() ' End the document. xml_text_writer.WriteEndDocument() xml_text_writer.Flush() ' Close the XmlTextWriter. xml_text_writer.Close() End Using ' memory_stream End Sub ' Add an Employee node to the document. Private Sub MakeEmployee(ByVal xml_text_writer As XmlTextWriter, ByVal first_name As String, ByVal last_name As String, ByVal emp_id As Integer) ' Start the Employee element. xml_text_writer.WriteStartElement("Employee") ' Write the FirstName. xml_text_writer.WriteStartElement("FirstName") xml_text_writer.WriteString(first_name) xml_text_writer.WriteEndElement() ' Write the LastName. xml_text_writer.WriteStartElement("LastName") xml_text_writer.WriteString(last_name) xml_text_writer.WriteEndElement() ' Write the EmployeeId. xml_text_writer.WriteStartElement("EmployeeId") xml_text_writer.WriteString(emp_id.ToString) xml_text_writer.WriteEndElement() ' Close the Employee element. xml_text_writer.WriteEndElement() End Sub End Class
创建的XML如下:↓
- XML code
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Employees> <Employee> <FirstName>Albert</FirstName> <LastName>Anders</LastName> <EmployeeId>11111</EmployeeId> </Employee> <Employee> <FirstName>Betty</FirstName> <LastName>Beach</LastName> <EmployeeId>22222</EmployeeId> </Employee> <Employee> <FirstName>Chuck</FirstName> <LastName>Cinder</LastName> <EmployeeId>33333</EmployeeId> </Employee> </Employees>
现在我想分别得到FirstName、LastName、EmployeeId中的数据,具体怎么读取
能直接用XmlTextReader吗?越具体越好!谢谢
我是用这个XML保存我的程序的数据,用户关闭程序后再打开程序做到读取XML中信息
新人,多多关照!
------解决方案--------------------
这个xml的文档的格式有点问题,没有root.
应该是
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Compnay>
<Employees>
<Employee>
<FirstName>Albert</FirstName>
<LastName>Anders</LastName>
<EmployeeId>11111</EmployeeId>
</Employee>
<Employee>
<FirstName>Betty</FirstName>
<LastName>Beach</LastName>