我正在尝试在 vb.net 中使用 xml linq xdocument

发布于 2024-09-26 05:57:47 字数 1578 浏览 2 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-10-03 05:57:47

您的 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。如果您想投影到匿名类型,则需要使用 With New { .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">

然后,您必须修改代码以获取命名空间并将其与元素连接起来,如下所示:

Dim ns = xml.Root.GetDefaultNamespace()

Dim products = From product In xml.Descendants(ns + "product") _
               Select product.Element(ns + "title").Value

Your LINQ statement is not projecting into an anonymous type with a property of Title. You're getting an IEnumerable<string> back directly.

Try this instead:

Dim products = From product In xml.Descendants("product") _
               Select product.Element("title").Value

For Each product In products
     Debug.WriteLine("Title: " & product)
Next

That said, the variable products is better named titles. If you want to project into an anonymous type you need to use With New { .Prop1 = data, .Prop2 = other }. A dot must be prefixed to each property name. Here's an example:

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

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:

<productslist xmlns="http://domain.com/namespace">

You would then have to modify your code to get the namespace and concatenate it with your elements as follows:

Dim ns = xml.Root.GetDefaultNamespace()

Dim products = From product In xml.Descendants(ns + "product") _
               Select product.Element(ns + "title").Value
太阳公公是暖光 2024-10-03 05:57:47

抱歉,我不懂 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 缺少 。节点(结束标记),我希望这只是一个复制粘贴错误。

Sorry, I don't know VB But in C# this code will do-

var query = doc.Descendants("product").Select(x => new {Title= x.Element("title").Value});

            foreach (var item in query)
            {
                Console.WriteLine(item.Title);

            }

Also the xml you posted is missing </products>. node (end tag), I hope it's just a copy paste mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文