1

我有一个看起来像这样的 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 的客户列表。我究竟做错了什么?

谢谢

4

1 回答 1

0

我同意史蒂夫狗的观点。您可以使用 Xml 序列化程序来执行此操作。这是一些示例代码:

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...
于 2012-05-29T15:27:29.360 回答