读取嵌套的xml文件

时间:2012-05-29 13:12:02

标签: vb.net linq-to-xml

我有一个xml文件,如下所示:

<SalesReps>
  <SalesRep>
    <repname> Bob</repname>
      <repid>100</repid>
      <customers>
        <customer>
          <custname>ABC Company</custname>
          <custno>51233</custno>
        </customer>
        <customer>
          <custname>XYZ Inc.</custname>
          <custno>29943</custno>
        </customer>
      </customers>
    </SalesRep>
  <SalesRep>
    <repname>Sue</repname>
      <repid>43</repid>
      <customers>
        <customer>
          <custname>Petes Tire Co</custname>
          <custno>49999</custno>
        </customer>
        <customer>
          <custname>Suzy's Sewing</custname>
          <custno>81234</custno>
        </customer>
      </customers>
    </SalesRep>
</SalesReps>

我正在尝试使用以下代码阅读它:

Dim salesreps = From reps In xe.Descendants("SalesReps") Select reps
Dim el = (From rep In salesreps _
Select New With {.repname = rep.<repname>, _
                 .repid = rep.<repid>,
                 .customers = (From custs In rep.<Customers> _
                   Select New With { _
                   .customer = ( _
                       From cust In custs.<customer> _
                           Select New With {
                           .custname = cust.<custname>.Value, _
                           .custno = cust.<custno>.Value} _
                       )} _
                  ) _
              } _
)

我可以获得repname和repid,但是没有获得custname和custno的客户列表。我做错了什么?

THX

1 个答案:

答案 0 :(得分:0)

我同意SteveDog。您可以使用Xml Serializer执行此操作。以下是一些示例代码:

Imports System.Xml.Serialization

<XmlRoot("SalesReps")> _
Public Class SalesReps
    Inherits List(Of SalesRep)
End Class

Public Class SalesRep
    Public Property RepName As String
    Public Property RepId As Integer

    <XmlArray("Customers"), XmlArrayItem("Customer")> _
    Public Property Customers As List(Of Customer)

    Public Sub New()
        Me.Customers = New List(Of Customer)()
    End Sub
End Class

Public Class Customer
    Public Property CustName As String
    Public Property CustNo As String
End Class

Module Module1                                                                                    
    Sub Main()                                                                                    

        Dim Reps As New SalesReps()                                                               

        Dim rep As New SalesRep With {.RepId = 100, .RepName = "Bob"}                             
        rep.Customers.Add(New Customer With {.CustName = "ABC Company", .CustNo = "51233"})       
        rep.Customers.Add(New Customer With {.CustName = "XYZ Inc.", .CustNo = "29943"})          
        Reps.Add(rep)                                                                             

        rep = New SalesRep With {.RepId = 43, .RepName = "Sue"}                                   
        rep.Customers.Add(New Customer With {.CustName = "Petes Tire Company", .CustNo = "49999"})
        rep.Customers.Add(New Customer With {.CustName = "Suzy's Sewing", .CustNo = "81243"})     
        Reps.Add(rep)                                                                             

        Dim sb As New StringBuilder()                                                             

        Dim xSer As New XmlSerializer(GetType(SalesReps))                                         
        Using wrt As New StringWriter(sb)                                                         
            xSer.Serialize(wrt, Reps)                                                             
        End Using                                                                                 

        Console.WriteLine(sb.ToString())                                                          

        Dim NewReps As SalesReps = Nothing                                                        
        Using rdr As New StringReader(sb.ToString())                                              
            NewReps = xSer.Deserialize(rdr)                                                       
        End Using                                                                                 

        If NewReps IsNot Nothing Then                                                             
            Dim firstRep = NewReps.First()                                                        

            Console.WriteLine(firstRep.RepName)                                                   
            For Each cust As Customer In firstRep.Customers                                       
                Console.WriteLine("   {0}", cust.CustName)                                        
            Next                                                                                  
        End If                                                                                    

        Console.WriteLine("Press ENTER to exit...")                                               
        Console.ReadLine()                                                                        
    End Sub                                                                                       
End Module

产生此输出:

<?xml version="1.0" encoding="utf-16"?>
<SalesReps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SalesRep>
    <RepName>Bob</RepName>
    <RepId>100</RepId>
    <Customers>
      <Customer>
        <CustName>ABC Company</CustName>
        <CustNo>51233</CustNo>
      </Customer>
      <Customer>
        <CustName>XYZ Inc.</CustName>
        <CustNo>29943</CustNo>
      </Customer>
    </Customers>
  </SalesRep>
  <SalesRep>
    <RepName>Sue</RepName>
    <RepId>43</RepId>
    <Customers>
      <Customer>
        <CustName>Petes Tire Company</CustName>
        <CustNo>49999</CustNo>
      </Customer>
      <Customer>
        <CustName>Suzy's Sewing</CustName>
        <CustNo>81243</CustNo>
      </Customer>
    </Customers>
  </SalesRep>
</SalesReps>
Bob
   ABC Company
   XYZ Inc.
Press ENTER to exit...