i';我正在尝试在vb.net中使用xml linq xdocument
我正在尝试编写一个WindowsPhone7应用程序,它使用xdocument读取xml,但我遇到了一些问题 如果我这样做:i';我正在尝试在vb.net中使用xml linq xdocument,.net,vb.net,windows-phone-7,linq-to-xml,.net,Vb.net,Windows Phone 7,Linq To Xml,我正在尝试编写一个WindowsPhone7应用程序,它使用xdocument读取xml,但我遇到了一些问题 如果我这样做: Dim xml As XDocument = XDocument.Load(e.Result) System.Diagnostics.Debug.WriteLine(xml.ToString) 或者这个: System.Diagnostics.Debug.WriteLine(xml.Elements.Value.ToString) 然后xml数据作为字符串输出到imm
Dim xml As XDocument = XDocument.Load(e.Result)
System.Diagnostics.Debug.WriteLine(xml.ToString)
或者这个:
System.Diagnostics.Debug.WriteLine(xml.Elements.Value.ToString)
然后xml数据作为字符串输出到immidate窗口,以证明数据存在,但如果我这样做:
Dim products = From product In xml.Descendants("product") _
Select title = product.Element("title").Value
For Each product In products
System.Diagnostics.Debug.WriteLine("Title" & product.title)
Next
我对product.title一无所获,我做这样的事情也一无所获:
Dim count As Integer = xml.Descendants("count").Value
<productslist>
<count>2</count>
<products>
<product>
<productId>1</productId>
<title>test item 1 </title>
<price>4.99</price>
<category>
<categoryId>1</categoryId>
<categoryName>cat 1</categoryName>
</category>
</product>
<product>
<productId>2</productId>
<title>test item 2</title>
<price>10.99</price>
<category>
<categoryId>2</categoryId>
<categoryName>cat 2</categoryName>
</category>
</product>
</productslist>
我做错了什么?
谢谢
xml如下所示:
Dim count As Integer = xml.Descendants("count").Value
<productslist>
<count>2</count>
<products>
<product>
<productId>1</productId>
<title>test item 1 </title>
<price>4.99</price>
<category>
<categoryId>1</categoryId>
<categoryName>cat 1</categoryName>
</category>
</product>
<product>
<productId>2</productId>
<title>test item 2</title>
<price>10.99</price>
<category>
<categoryId>2</categoryId>
<categoryName>cat 2</categoryName>
</category>
</product>
</productslist>
2.
1.
测试项目1
4.99
1.
第一类
2.
测试项目2
10.99
2.
第2类
您的LINQ语句没有投影到属性为Title
的匿名类型。您将直接获得一个IEnumerable
请尝试以下方法:
Dim products = From product In xml.Descendants("product") _
Select product.Element("title").Value
For Each product In products
Debug.WriteLine("Title: " & product)
Next
也就是说,变量products
最好命名为titles
。如果要投影到匿名类型,则需要对新的{.Prop1=data、.Prop2=other}使用。每个属性名称的前缀必须为点。下面是一个例子:
Dim products = From product In xml.Descendants("product") _
Select New With { .Title = product.Element("title").Value }
For Each product In products
Debug.WriteLine("Title: " & product.Title)
Next
对于您的示例,似乎不需要匿名类型。如果您有多个属性要投射到其中,那么它就变得很有价值
编辑:为了响应您的评论,可能会在XML中附加一个名称空间。如果是这样,您将需要引用它来引用任何元素
如果XML具有名称空间,则应指定xmlns
,例如:
<productslist xmlns="http://domain.com/namespace">
抱歉,我不懂VB,但在C中,这段代码就可以了-
var query = doc.Descendants("product").Select(x => new {Title= x.Element("title").Value});
foreach (var item in query)
{
Console.WriteLine(item.Title);
}
您发布的xml也丢失了
。节点(结束标记),我希望这只是一个复制粘贴错误。当我们不知道文档的外观时,很难知道您做错了什么。你能把你的问题编辑成包括在内吗?谢谢你的回答,但我还是一无所获。我检查了products.Count,它返回0,因此for-each语句的fire@JasperS真奇怪。您能否设置断点并检查xml.subjections(“产品”)
以确保返回这些节点?有没有可能您的XML使用了名称空间,并且与上面显示的方式不完全相同?名称空间需要在元素名称前加前缀。我不理解xml中名称空间的含义,我对xml没有太多的了解。元素名称的前缀是什么意思?