Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML数据到数组_C#_Xml_Arrays_Linq - Fatal编程技术网

C# XML数据到数组

C# XML数据到数组,c#,xml,arrays,linq,C#,Xml,Arrays,Linq,下面是我从URL获取的XML数据。当我查看源代码时,下面是它的外观: <xml> <beginning> <id>information from rss provider here</id> <updated>information from rss provider here</updated> <link rel='alternate' type='tex

下面是我从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>

这里有来自rss提供商的信息
这里有来自rss提供商的信息
测试
测试
十来岁
测试
测试
测试
蒂斯特
测试
测试
测试
十来岁
测试
测试
测试
蒂斯特
测试
测试
测试
十来岁
测试
测试
测试
蒂斯特
测试
我能够阅读消息框中的内容:

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());
    }                 
}
WebRequest-request=WebRequest.Create(“http://www.test.com/file.xml");
WebResponse=request.GetResponse();
Stream dataStream=response.GetResponseStream();
XElement=XElement.Load(数据流);
IEnumerable employees=xelement.Elements();
foreach(员工中的var员工)
{
if(employee.Elements(“内容”)!=null)
{
Show(employee.Value.ToString());
}                 
}
我想将其保存到数组、列表或LINQ

如何将上面的代码与上面的XML一起使用,并将其生成一个数组

我只需要
中的所有数据,并且只需要这些值作为键/值对:

<title type='html'>test</title>
    <summary type='html'>test</summary>
    <descriptions type='html'>test</descriptions>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
测试
测试
测试
蒂斯特
测试

以下是LINQ到XML的版本:

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

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

以下是LINQ到XML的版本:

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

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

为了完整性,您需要创建自己的类来存储每个
SomeSchema
对象

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

为了完整性,您需要创建自己的类来存储每个
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();

那么为什么不创建一个
Employee
类呢?你已经在循环东西了,只要把它添加到一个类中,给它一些语义,你就可以排序了。例如,不要显示
employee.Value
,而是将其添加到
列表中。嗨,Arran,我希望该值位于键/值对中。谢谢。请注意
WebResponse
Stream
IDisposable
资源,因此应该在
中使用
块。那么为什么不创建一个
Employee
类呢?你已经在循环东西了,只要把它添加到一个类中,给它一些语义,你就可以排序了。例如,不要显示
employee.Value
,而是将其添加到
列表中。嗨,Arran,我希望该值位于键/值对中。谢谢。请注意
WebResponse
Stream
IDisposable
资源,因此应该在
中使用
块。很好,除了当变量为
XElement
时调用
ToArray()
。返回类型应该是
string[]
。我收到一个错误,说您无法将类型string[]隐式转换为system.xml.linq.xlelementgreat Thank,所以现在我可以遍历“名称”并访问键/值对?@Menew,为此,您需要编辑您的帖子以定义键/值对。您可以在for/foreach循环中使用您的
名称
。@Nirmal您的答案也是正确的,但是,我需要创建一个详细的键/值对。很好,除了在变量为
XElement时调用
ToArray()
。返回类型应该是
string[]
。我收到一个错误,说您无法将类型string[]隐式转换为system.xml.linq.xlelementgreat Thank,所以现在我可以遍历“名称”并访问键/值对?@Menew,为此,您需要编辑您的帖子以定义键/值对。您可以在for/foreach循环中使用您的
名称
。@Nirmal您的答案也是正确的,但是,我需要创建一个详细的键/值对。