2016-12-08 9 views
0

XElementコレクションから文字列値のリストを返したいが、コードを構築するときにこのエラーが発生し、 。 enter image description hereXElementの「コンテキストインスタンス」where句を使用したXMLクエリへのlinqの呼び出し

はここで私が問題をconcering書いたクラスのセクションです:

private XElement _viewConfig; 

public ViewConfiguration(XElement vconfig) 
{ 

    _viewConfig = vconfig; 
} 

public List<string> visibleSensors() 
{ 

    IEnumerable<string> sensors = (from el in _viewConfig 
            where el.Attribute("type").Value == "valueModule" 
             && el.Element.Attribute("visible") = true 
            select el.Element.Attribute("name").Value); 

    return sensors.ToList<string>(); 
} 

XElementのコレクションが

<module name="temperature" type="valueModule" visible="true"></module> 
<module name="lightIntensity" type="valueModule" visible="true"></module> 
<module name="batteryCharge" type="valueModule" visible="true"></module> 
<module name="VsolarCells" type="valueModule" visible="false"></module> 

答えて

2

形式であるすべてのXElementの第一は、それゆえIEnumerableではありません最初の行from el in _viewConfigは無効です。これが有効なXMLファイルからのものである場合、私は<module>要素が親要素の内部に含まれていると推定します(例:<modules>)。あなたは_viewConfigmodulesを指すようにする場合は、次のように動作します:

IEnumerable<string> sensors = (
    from el in _viewConfig.Elements() 
    where el.Attribute("type").Value == "valueModule" 
      && el.Attribute("visible").Value == "true" 
    select el.Attribute("name").Value); 

をまたそれゆえ、それは私も上記の(と一緒にから削除されElementというプロパティを持っていない、elのそのタイプがXElementであることに注意します比較のために=の代わりに==を使用し、属性テキスト値の値を比較するためにブール値trueの代わりに文字列リテラル"true"を使用して修正しなければならなかった構文エラーはほとんどありません。

+0

ありがとうございました。私は組み込みを開始し、システムを完成させる。 – maynard

関連する問題