2011-05-27 111 views
0

我想用linq查询xml的一些信息,但是我得到这样的错误 - 是的,我已经定义 - 使用System.Linq 你能告诉我,问题在哪里? 感谢用linq查询xml

错误1找不到一个 实现查询模式 的源类型 “urn.P.IEEE.Item1671.Item2.Item2008.Item02.InstrumentDescription.InstrumentDescription”。 '找不到'。 d:\ Documents和 设置\ e539951 \我的文档\ Visual 工作室 2010 \项目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs中28 36 WindowsFormsApplication1

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

看起来更像是使用linq查询对象,什么是'InstrumentDescription',它不是'XDocument'? – Jodrell 2011-05-27 16:30:57

+1

是InstrumentDescription IQueryable? – Jason 2011-05-27 16:33:32

+0

InstrumentDescription是用linqtoxsd生成的.cv文件,然后用xsd生成代码 – 2011-05-27 16:36:00

回答

1

在您的代码中test仅代表文档的根元素,因此您无法在其上使用LINQ--它不是一个序列。

你应该做什么取决于你的XSD的样子。例如,如果根InstrumentDescription元素下可能有多个Identification元素,则只需访问test.Identitication即可获得该列表。

0

您正在操作InstrumentDescription,而不是一个XDocument所以这可能是你需要确保你的InstrumentDescription类是IQueryable。

如果您确实想要对您的XML执行Linq,您需要将它作为数据集加载或使用Linq2XML(using System.Xml.Linq)。

查看更多这里。 http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx