views:

26

answers:

1

Hi Guys,

I am Fairly new to Linq and I am trying to write a simple query to return the error messages within my xml file.

<?xml version="1.0" encoding="utf-8"?>
<Error xmlns="urn:xxxxx">
            The following errors were detected:
            <Message>Internal Error</Message></Error>

The following works and returns the error message:

     Dim loaded As XDocument = XDocument.Parse(strReturn)
     Dim ns As XNamespace = "urn:xxxxx"
     Dim errors = From err In loaded.Descendants(ns + "Error") _
                                   Select err.Elements(ns + "Message").Value

but the following does not and returns no results:

Imports <xmlns="urn:xxxxx">
Dim loaded As XDocument = XDocument.Parse(strReturn)
Dim errors = From err In loaded.Descendants.<Error> _
                                 Select err.Elements.<Message>.Value

I am just trying to get a better understand of Linq but can someone tell me why the later does not work as should they both not return the same result?

Thanks in advance

A: 

Writing .<Error> makes a call to the Elements method, so when you call err.Elements.<Message> you are doing err.Elements.Elements("Message") and getting the children of the children of err instead of just its children. Just remove the extra calls to Descendants and Elements. You will need to use ... instead of . if you want descendants of loaded instead of just children.

Dim errors = From err In loaded...<Error> _
             Select err.<Message>.Value
Quartermeister
Thanks Quartermeister perfect
fedor333