我有一个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
我还没有与LINQ熟悉直接回答你的问题,但我想问问你,如果你有或将考虑使用XML反序列化来读取这个XML文档? LINQ是查询文档某些部分的一种便捷方式,但如果您只是阅读整个文档,看起来工作量很大,复杂性和易碎性。 –