我正在尝试在 vb.net 中使用 xml linq xdocument
我正在尝试编写一个 Windows Phone 7 应用程序,它使用 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 数据作为字符串输出到即时窗口,证明数据存在,但如果我这样做:
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
我做错了什么? 谢谢。
xml 看起来像这样:
<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>
I'm trying to write a windows phone 7 app which reads from an xml using xdocument but i'm having a few problems.
If I do this:
Dim xml As XDocument = XDocument.Load(e.Result)
System.Diagnostics.Debug.WriteLine(xml.ToString)
Or this:
System.Diagnostics.Debug.WriteLine(xml.Elements.Value.ToString)
then the xml data is output to the immidiate window as a string proving that the data exists but if i do this:
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
I get nothing for product.title and I also get nothing when I do stuff like this:
Dim count As Integer = xml.Descendants("count").Value
What am I doing wrong?
Thanks.
xml looks something like this:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 LINQ 语句不会投影到具有
Title
属性的匿名类型。您将直接返回IEnumerable
。试试这个:
也就是说,变量
products
最好命名为titles
。如果您想投影到匿名类型,则需要使用With New { .Prop1 = data, .Prop2 = other }
。每个属性名称都必须添加一个点作为前缀。这是一个示例:对于您的示例,似乎不需要匿名类型。如果您有多个属性可供投影,那么它就变得值得。
编辑:为了响应您的评论,命名空间可能会附加到您的 XML 中。如果是这样,您将需要引用它来引用任何元素。
如果您的 XML 有命名空间,则应指定
xmlns
,例如:然后,您必须修改代码以获取命名空间并将其与元素连接起来,如下所示:
Your LINQ statement is not projecting into an anonymous type with a property of
Title
. You're getting anIEnumerable<string>
back directly.Try this instead:
That said, the variable
products
is better namedtitles
. If you want to project into an anonymous type you need to useWith New { .Prop1 = data, .Prop2 = other }
. A dot must be prefixed to each property name. Here's an example:For your example it doesn't seem that an anonymous type is needed. If you have multiple properties to project into then it becomes worthwhile.
EDIT: in response to your comment, a namespace might be attached to your XML. If so, you would need to reference it to reference any element.
If your XML has a namespace it should have an
xmlns
specified, such as:You would then have to modify your code to get the namespace and concatenate it with your elements as follows:
抱歉,我不懂 VB,但在 C# 中,这段代码就可以了 -
而且您发布的 xml 缺少
。节点(结束标记),我希望这只是一个复制粘贴错误。
Sorry, I don't know VB But in C# this code will do-
Also the xml you posted is missing
</products>
. node (end tag), I hope it's just a copy paste mistake.