0

下面是我从 URL 获取的 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>
4

2 回答 2

4

这是一个 LINQ to XML 版本:

string[] names = XElement.Load(dataStream).Descendants("Name")
                        .Select(element => element.Value).ToArray();

这将给出Name文档中的所有元素。

于 2013-09-30T14:03:24.107 回答
1

为了完整起见,您需要创建自己的类来存储每个someschemas对象。

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}
}

然后循环并添加您的项目

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可以有多个条目,那么您需要在List内部创建一个对象SomeSchemas

然后你可以items在一个循环中使用,并在它上面做 linq。它不会是键/值对(如 a dictionary),但您可以根据其 ID 选择一个项目(如果它们是唯一的)。

SomeSchemas myObject = items.Single(x => x.ID == "asdf");
于 2013-09-30T14:29:56.033 回答