XML数据到数组 [英] XML data to array
问题描述
以下是我从URL获得的XML数据.当我查看源代码时,它是这样的:
Below is the XML data I am getting from a URL. When I view source, here is how it looks:
<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>
我能够阅读消息框中的内容:
I am able to read the content in a message box:
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中.
I would like to save this to an array, or a list, or LINQ.
如何将上面的代码与上面的XML结合使用,并使其成为数组.
How can i use the code above with the XML above and make it into an array.
我只希望<someschemas>.
中的所有数据,并且只将这些值作为键/值对:
I only want all the data within <someschemas>.
and just these values as key/value pairs:
<title type='html'>test</title>
<summary type='html'>test</summary>
<descriptions type='html'>test</descriptions>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
推荐答案
为完整起见,您需要创建自己的类来存储每个someschemas
对象.
For completeness, you need to make your own class to store each someschemas
object.
class SomeSchemas
{
public string ID {get;set;}
public string MovieId {get;set;}
//add in others here
public string Actor_Name {get;set;}
public string Actor_Phone {get;set}
}
然后遍历并添加您的物品
Then loop through and add your items
List<SomeSchemas> items = XElement.Load(dataStream)
.Descendants("someschemas")
.Select(x => new SomeSchemas()
{
ID = x.Element("id").Value,
MovieId = x.Element("movieid").Value,
//add in others here
Actor_Name = x.Element("actor").Element("name").Value,
Actor_Phone = x.Element("actor").Element("phone").Value
}.ToList();
如果Actors
可以有多个条目,则需要在SomeSchemas
内创建一个List
对象.
If Actors
can have more than one entry, then you need to make a List
object inside SomeSchemas
.
然后您可以循环使用items
并对其执行linq.它不会是键/值对(例如dictionary
),但是如果它们是唯一的,则可以根据其ID选择一个项目.
Then you can use items
in a loop, and do linq on it. It won't be a key/value pair (like a dictionary
), but you can select an item based on its ID, if they are unique.
SomeSchemas myObject = items.Single(x => x.ID == "asdf");
这篇关于XML数据到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!