简体   繁体   English

如何使用XmlReader读取列表中的数据

[英]How to use XmlReader to read data in a List

I'd like to take the contents of an XML file and dump it into a list. 我想获取XML文件的内容并将其转储到列表中。 Then I'd like to take the strings in the list and parse them for data using XmlReader. 然后,我想获取列表中的字符串,并使用XmlReader解析它们以获取数据。 Is there any way to do this? 有什么办法吗?

I'm not very familiar with the XmlReader, and no where near an expert with C#, but I'd like to use something like this: 我对XmlReader不太熟悉,并且几乎没有C#专家,但是我想使用如下代码:

List<string>fileData = new List<string>();
string xmlData = "somestring";
XmlReader reader = XmlReader.Create(new StringReader(xmlData));

/* put all the data in a file into fileData */

foreach (string s in fileData)
{
   /* use reader.WHATEVERFUNCTION I need to parse fileData for my data */
}

Is there a way to do this? 有没有办法做到这一点? It seems like I'd have to put the .Create function inside the foreach, which means that I'd have to create a new reader every time I need to use a function. 似乎我必须将.Create函数放到foreach中,这意味着每次需要使用函数时,都必须创建一个新的阅读器。 That doesn't seem like the best of ideas for many reasons. 由于许多原因,这似乎并不是最好的主意。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

Depending on the xml you're working with: 根据您使用的xml:

XDocument doc = XDocument.Parse("<root><node>value1</node><node>value2</node></root>");
List<String> data = doc.Root.Elements().Select(e => e.Value).ToList();

您应该看看System.Xml.Linq命名空间,它实际上很容易使用: http : //msdn.microsoft.com/zh-cn/library/bb343181.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM