首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

VB.NET 读取XML中的某一子节点中的信息解决方法

2012-08-27 
VB.NET 读取XML中的某一子节点中的信息写了如下代码,创建了一个XMLVB.NET codeImports System.XmlImports

VB.NET 读取XML中的某一子节点中的信息
写了如下代码,创建了一个XML

VB.NET code
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如下:↓
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>


<EmployeeId>22222</EmployeeId>
</Employee>
<Employee>
<FirstName>Chuck</FirstName>
<LastName>Cinder</LastName>
<EmployeeId>33333</EmployeeId>
</Employee>
</Employees>
</Compnay>
[解决办法]
按ls的xml,Serialize和Deserialize都比较简单。

C# code
         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#强太多了。
VB.NET code
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 

热点排行