以下是我從URL獲得的XML數據。當我查看源代碼,這裏是它的外觀:XML數據到數組
<xml>
<beginning>
<id>information from rss provider here</id>
<updated>information from rss provider here</updated>
<link rel='alternate' type='text/html' href='tttt' title='alternate'/>
<link rel='self' type='application/atom+xml' href='tttt'/>
<someschemas>
<id>test</id>
<movieid>test</movieid>
<updated>teeeeest</updated>
<types scheme='vals' term='test'/>
<title type='html'>test</title>
<summary type='html'>test</summary>
<descriptions type='html'>test</descriptions>
<link rel='alternate' type='text/html' href='tttt' title='alternate'/>
<link rel='self' type='application/atom+xml' href='tttt'/>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
</someschemas>
<someschemas>
<id>test</id>
<movieid>test</movieid>
<updated>teeeeest</updated>
<types scheme='vals' term='test'/>
<title type='html'>test</title>
<summary type='html'>test</summary>
<descriptions type='html'>test</descriptions>
<link rel='alternate' type='text/html' href='tttt' title='alternate'/>
<link rel='self' type='application/atom+xml' href='tttt'/>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
</someschemas>
<someschemas>
<id>test</id>
<movieid>test</movieid>
<updated>teeeeest</updated>
<types scheme='vals' term='test'/>
<title type='html'>test</title>
<summary type='html'>test</summary>
<descriptions type='html'>test</descriptions>
<link rel='alternate' type='text/html' href='tttt' title='alternate'/>
<link rel='self' type='application/atom+xml' href='tttt'/>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
</someschemas>
</beginning>
</xml>
我能讀在消息框中的內容:
WebRequest request = WebRequest.Create("http://www.test.com/file.xml");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
XElement xelement = XElement.Load(dataStream);
IEnumerable<XElement> employees = xelement.Elements();
foreach (var employee in employees)
{
if (employee.Elements("content") != null)
{
MessageBox.Show(employee.Value.ToString());
}
}
我想這個保存到一個數組,或列表,或者LINQ。
我怎樣才能使用上面的代碼與上面的XML,並把它變成一個數組。
我只想內<someschemas>.
的所有數據,只是這些值作爲鍵/值對:
<title type='html'>test</title>
<summary type='html'>test</summary>
<descriptions type='html'>test</descriptions>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
那麼你爲什麼不創建一個Employee類呢?你已經循環了一些東西,只需將它添加到一個類中,給出一些語義含義,然後對其進行排序。例如,而不是*顯示*'employee.Value',*將它添加到'List'中。 – Arran
嗨Arran,我想價值是在鍵/值對。謝謝。 – NULL
請注意'WebResponse'和'Stream'是'IDisposable'資源,因此應該在'using'塊中。 –