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

私はSteveDogに同意します。これを行うには、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 に答える