Solved

Problem querying xml file

Posted on 2010-11-13
3
433 Views
Last Modified: 2013-11-11
I am attempting to query the xml file to return the Tasks using the following code which is not returning anything! Is it possible to use linq to query an xml file of this structure?

Code:
  Dim doc As XDocument = XDocument.Load(XMLDOCFILEPATH)
            Dim qList = From xe In doc.XPathSelectElements("//Tasks") _
                    Select New With { _
                            .TaskId = xe.Attribute("TaskId").Value, _
                            .Question = xe.Attribute("Question").Value, _
                            .Result = xe.Element("Result").Value, _
                            .Comment = xe.Element("Comment").Value _
                            }
            Me.DataGridView1.DataSource = qList. engineereventVCAtwork.xml
0
Comment
Question by:JoeBo747
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34127463
Hi JoeBo747;

Made some changes to your code. Also your XML document has a Namespace and you need to account for it in the query as shown below.

Fernando
Dim doc As XDocument = XDocument.Load(XMLDOCFILEPATH)

' You need to account for the Namespace in the document
Dim ns As XNamespace = "http://mycomp.co.uk/Sexton.xsd/v2"

Dim qList = From xe In doc.Descendants(ns + "Task") _
            Select New With { _
                    .TaskId = xe.Element(ns + "TaskId").Value, _
                    .Question = xe.Element(ns + "Question").Value, _
                    .Result = xe.Element(ns + "Result").Value, _
                    .Comment = xe.Element(ns + "Comment").Value _
                }

Me.DataGridView1.DataSource = qList.ToList

Open in new window

0
 
LVL 1

Author Closing Comment

by:JoeBo747
ID: 34127537
Thanks Fernando thats great.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34127553
Not a problem, glad I was able to help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question