2017-04-06 7 views
0

こんにちは。私のXMLからatribute環境をフィルタリングすることによって接続を取得したいです。 しかし、私はgettinいくつかのエラーが誰でも助けることができます。ここOpen XMLをLinqで開く方法

は私のコードです:

XElement xelement = XElement.Load(xml); 
 
var Connections = from conn in xelement.Elements("Conections") 
 
        where (String)conn.Element("Conection").Attribute("Enviroment") == "Test" 
 
        select conn; 
 

 
foreach (XElement conection in Connections) 
 
{ 
 
    MessageBox.Show(conection.Element("Conection").Value); 
 
}

。ここでは私のxmlです

<?xml version="1.0" encoding="utf-8"?> 
 
<Config> 
 
    <General> 
 
    <FormaInicial></FormaInicial> 
 
    <DiasRecordatorioBuro></DiasRecordatorioBuro> 
 
    <EnviarSMSDomiciliacion>rue</EnviarSMSDomiciliacion> 
 
    <SimularDeathLock></SimularDeathLock> 
 
    <IsProductionEnviroment></IsProductionEnviroment> 
 
    <WaitingTimeBetweenExecutions></WaitingTimeBetweenExecutions> 
 
    <NumberTriesBeforeRestartService></NumberTriesBeforeRestartService> 
 
    </General> 
 
    <Conections> 
 
    <Conection Enviroment="Production"> 
 
     <Servidor></Servidor> 
 
     <BaseDatos>==</BaseDatos> 
 
     <Usuario></Usuario> 
 
     <Password></Password> 
 
    </Conection> 
 
    <Conection Enviroment="Test"> 
 
     <Servidor></Servidor> 
 
     <BaseDatos></BaseDatos> 
 
     <Usuario></Usuario> 
 
     <Password></Password> 
 
    </Conection> 
 
    </Conections> 
 
</Config>

+0

してくださいしかし、非HTML/JavaScriptの/ CSS:その後、あなたの結果では、あなたが「接続」タイプのXElementsのコレクションを持っています

var connections = xelement.Elements("Conections") .SelectMany(c => c.Elements("Conection")) .Where(c => c.Attribute("Enviroment").Value == "Test"); 

そして:あなたはSelectManyを使用して、最初のコレクションを平らにする必要があります'{}'を使って問題のコードを作成しましたが、質問のタイトルに 'how'のスペルが間違っています – Jpsh

+1

エラーは何ですか? – har07

答えて

0

あなたは(ほとんど)直面している問題です「接続」は「接続」要素の集合であり、属性でそれらをフィルタリングしようとしているときは、最初の要素のみをチェックします。

foreach (var conection in connections) 
{ 
    Console.WriteLine(conection); //a full element with Servidor, BaseDatos, Usuario and Password 
    Console.WriteLine(conection.Element("Servidor").Value);  
} 
+0

ありがとうございました。あなたは本当に役に立ちました –

関連する問題