VB.NET 读取XML中的某一子节点中的信息
写了如下代码,创建了一个XML
Imports System.XmlImports System.IOPublic 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 SubEnd Class<?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>
<EmployeeId>22222</EmployeeId>
</Employee>
<Employee>
<FirstName>Chuck</FirstName>
<LastName>Cinder</LastName>
<EmployeeId>33333</EmployeeId>
</Employee>
</Employees>
</Compnay>
[解决办法]
按ls的xml,Serialize和Deserialize都比较简单。
static void Main(string[] args) { string input = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?><Company><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></Company>"; XmlSerializer serializer = new XmlSerializer(typeof(Company)); using(StringReader sr = new StringReader(input)) { Company company = serializer.Deserialize(new StringReader(input)) as Company; using (StringWriter sw = new StringWriter()) { serializer.Serialize(sw, company); string res = sw.ToString(); } } } } [XmlRoot] public class Company { public List<Employee> Employees { get; set; } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public int EmployeeId { get; set; } }
[解决办法]
应该是
如果你尝试为你的xml定义或者生成xsd,会提示错误。
参考这里的讨论:
http://stackoverflow.com/questions/1945871/xmlserializer-deserializing-array-list-of-multiple-objects-at-root
[解决办法]
看我三楼代码Deserialize就是读取和解析成object
[解决办法]
其实用 Linq2Xml 也超方便,VB.NET 里对Xml的增强语法比C#强太多了。
Sub Main() Dim xml = <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> Dim employees = xml.<Employee>.Select(Function(n) New With {.FirstName = n.<FirstName>.Value, .LastName = n.<LastName>.Value}) For Each e In employees Console.WriteLine("{0} {1}", e.FirstName, e.LastName) Next Console.Read()End Sub