C# XML数据到数组
下面是我从URL获取的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
<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您的答案也是正确的,但是,我需要创建一个详细的键/值对。