John Earnshaw
John Earnshaw

Reputation: 331

i'm trying to work with xml linq xdocument in vb.net

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>

Upvotes: 0

Views: 6942

Answers (2)

Wondering
Wondering

Reputation: 5076

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.

Upvotes: 0

Ahmad Mageed
Ahmad Mageed

Reputation: 96487

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

Upvotes: 1

Related Questions