4

次のxmlがある場合:

        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")
                )
            )
        );

LINQ "を使用して"Hello、Foo"を出力した後です。表記。

使って「こんにちは」がもらえる

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

を使用してすべての属性を取得できます

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

属性の文字列値のリストを取得して、コンマ区切りのリストとして結合できるようにするにはどうすればよいですか?

4

2 に答える 2

2
var strings = from attribute in 
                       xDocument.Descendants("ChildElement").Attributes()
              select attribute.Value;
于 2009-09-02T16:15:22.000 に答える
2

さて、wompのおかげで、文字列の配列を取得できるようにプロパティValueを取得するために必要なSelectメソッドであることがわかりました。したがって、以下が機能します。

String.Join(",", (string[]) xDocument.Element("RootElement").Elements("ChildElement").Attributes("Attribute1").Select(attribute => attribute.Value).ToArray());
于 2009-09-02T16:46:09.877 に答える