views:

57

answers:

2

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>
+1  A: 

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"&gt;

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
Ahmad Mageed
Thanks for the reply but I still get nothing. I checked products.Count and it returns 0 so the for each statement doesn't even fire
JasperS
@JasperS that's odd. Can you set a break point and inspect `xml.Descendants("product")` to make sure those nodes are returned? Any chance your XML is using a namespace and doesn't exactly appear the way you showed it above? Namespaces need to be prefixed to element names.
Ahmad Mageed
I don't understand what you mean by namespace in xml, I've not worked with xml much. What do you mean by prefixed to element names?
JasperS
@JasperS see my edit for namespace information.
Ahmad Mageed
A: 

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.

Wondering