基本的linq到XML查询有问题

时间:2014-05-19 16:01:28

标签: xml vb.net linq linq-to-xml

我试图让一个非常简单的LINQ to XML查询工作,并且无法绕过我做错的事情。我在C#中做过类似的事情就好了,但由于某些原因,我在VB中遇到了同样的问题。

除了搜索此论坛之外,我还使用以下页面作为我的指南:http://msdn.microsoft.com/en-us/library/bb387053.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

对于我的XML,我有:

<?xml version="1.0" encoding="utf-8" ?>
<xmlroot>
    <customer>
        <name>test_customer</name>
        <data>data</data>
    </customer>
</xmlroot>

对于我的VB,我有:

Dim XMLFile As XDocument = XDocument.Load("file.xml")
Dim name_arg = "test_customer"

Dim customer As IEnumerable(Of XElement) = From el In XMLFile.Root.<customer> Where el.<name>.Value = name_arg Select el

If (customer.Count <= 0) Then
    Throw New Exception("Customer name not found in file.xml")
End If

我得到异常抛出,即使它看起来非常简单,它应该有效,即客户数应该是1.我也尝试过:

Dim customer As IEnumerable(Of XElement) = From el In XMLFile.Descendants("customer") Where el.Element("name").Value = name_arg Select el

没有运气。我做错了什么?

提前感谢任何见解。

编辑:

我也尝试过:

Dim root As XElement = XElement.Load("file.xml")

...加载我的XML文档,更像是msdn页面,并将我的查询更改为:

Dim customer As IEnumerable(Of XElement) = From el In root.<customer> Where el.<name>.Value = name_arg Select el

......仍然没有运气。

0 个答案:

没有答案
相关问题