8

ここでスタックオーバーフローに関するいくつかのスレッドを調べましたが、答えが見つかりません。次のようなxmlファイルの設定があります。

<entry id="1" type="a">
    <name>string 1</name>
    <description>any description</description>
</entry>
<entry id="2" type="b">
    <name>string 2</name>
    <description>any description #2</description>
</entry>

すべての「エントリ」タグを選択して、エントリのID、タイプ、内部名、説明タグを返す必要があります。C#でこれを行うにはどうすればよいですか?

ありがとう、

4

2 に答える 2

13

xmlファイルには単一のルートノードが必要であることに注意してください。これがLinqからXmlへの解析です。

var xdoc = XDocument.Load(path_to_xml);
var entries = from e in xdoc.Descendants("entry")
              select new {
                 Id = (int)e.Attribute("id"),
                 Type = (string)e.Attribute("type"),
                 Name = (string)e.Element("name"),
                 Description = (string)e.Element("description")
              };

クエリは、各エントリ要素に対応する匿名オブジェクトのシーケンスを返します(プロパティId、Type、Name、およびDescriptionを使用)。

于 2013-01-09T00:40:21.320 に答える
1

HtmlAgilityPackライブラリを見てください。これを使用すると、LINQまたはXPathを使用してHTMLを解析できます。

于 2013-01-09T00:32:48.443 に答える