2009-09-02 12 views
4

私は次のXML持っている場合: "" 私は、出力の "Hello、Fooの" LINQを使用した後だXDocument/LINQのCONCATENATE属性値

 XDocument xDocument = new XDocument(
      new XElement("RootElement", 
       new XElement("ChildElement", 
        new XAttribute("Attribute1", "Hello"), 
        new XAttribute("Attribute2", "World") 
       ), 
       new XElement("ChildElement", 
        new XAttribute("Attribute1", "Foo"), 
        new XAttribute("Attribute2", "Bar") 
       ) 
      ) 
     ); 

を表記

私は

xDocument.Element("RootElement").Element("ChildElement").Attribute("Attribute1").Value; 

を使用して「こんにちは」を得ることができます私は私ができるように、私は、属性の文字列値のリストを取得できますか

xDocument.Element("RootElement").Elements("ChildElement").Attributes("Attribute1"); 

を使用してすべての属性を取得することができますコンマで区切ったリストに参加しますか?

答えて

2

オクラホマので、おかげで、私はそれは私が、私は文字列の配列を得ることができるように、プロパティの値を取得するために必要な選択方法であったが実現wompします。したがって、以下のように動作する。

String.Join(",", (string[]) xDocument.Element("RootElement").Elements("ChildElement").Attributes("Attribute1").Select(attribute => attribute.Value).ToArray()); 
2
var strings = from attribute in 
         xDocument.Descendants("ChildElement").Attributes() 
       select attribute.Value; 
+0

私はそれを行う必要がありました。 Linqクエリを使用するのではなく、表記法。あなたが完全に正しい方向に私を指摘したように、Butr、+1。 –

+0

ああ、申し訳ありませんが、私は通常、慣習から外れたクエリ構文を使用します。 – womp