Query xml with linq

293 Views Asked by At

I am trying to query some informations from xml with linq but I am getting error like this - Yes I have defined - using System.Linq Could you tell me, where is a problem? Thanks

Error 1 Could not find an implementation of the query pattern for source type 'urn.P.IEEE.Item1671.Item2.Item2008.Item02.InstrumentDescription.InstrumentDescription'. 'Select' not found. D:\Documents and Settings\e539951\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 36 WindowsFormsApplication1

InstrumentDescription test = InstrumentDescription.Load(openFileDialog1.FileName);
 var query = from b in test
             select  new {  b.Identification };
2

There are 2 best solutions below

0
svick On BEST ANSWER

In your code test represents just the root element of the document, so you can't use LINQ on it – it's not an sequence.

What you should do depends on how your XSD looks like. For example, if there can be multiple Identification elements under the root InstrumentDescription element, then just accessing test.Identitication gives you the list.

0
AllenG On

You're handling InstrumentDescription instead of an XDocument so it's probably that you need to make sure you InstrumentDescription class is IQueryable.

If you're actually wanting to do Linq against your XML, you either need to load it in as a dataset, or use Linq2XML (using System.Xml.Linq).

See more here. http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx