Solved

Problem querying xml file

Posted on 2010-11-13
3
427 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 62

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 62

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now