2011-05-27 105 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