C# 使用 XmlTextReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10150785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using XmlTextReader
提问by Jess
I am a beginner programmer starting off with C#, and web services.
我是一名初学 C# 和 Web 服务的程序员。
In the Service.csfile of my web service, I create a ReadXMLFile()method where I am trying to read an existing XML file, take the data from it and place it to the corresponding properties (DataMembers) that I created in the IService.csfile.
在Service.cs我的 Web 服务文件中,我创建了一个ReadXMLFile()方法,我试图在其中读取现有的 XML 文件,从中获取数据并将其放置到我在IService.cs文件中创建的相应属性 (DataMembers) 中。
My problem is that my code is basically not doing anything. I've tried looking for web sites and tutorials on this but there really isn't much out there, especially for a beginner like myself. Anyone have any idea how I should go about this, because what I've been trying so far is obviously wrong.
我的问题是我的代码基本上没有做任何事情。我曾尝试寻找有关此的网站和教程,但实际上并没有太多内容,尤其是对于像我这样的初学者。任何人都知道我应该怎么做,因为到目前为止我一直在尝试的东西显然是错误的。
Below is my ReadXMLFile()method.
下面是我的ReadXMLFile()方法。
void ReadXMLFile()
{
XmlTextReader reader = new XmlTextReader("ClassRoll.xml");
reader.Read();
while (reader.Read())
{
if (reader.Name == "id")
{
id = reader.ReadString();
}
else if (reader.Name == "firstname")
{
link = reader.ReadString();
}
else if (reader.Name == "lastname")
{
description = reader.ReadString();
}
else if (reader.Name == "count")
{
description = reader.ReadString();
}
else if (reader.Name == "testscore")
{
description = reader.ReadString();
}
}
}
this is an example of my xml file
这是我的 xml 文件的示例
<classroll>
<student>
<id>101010</id>
<lastname>Smith</lastname>
<firstname>Joe</firstname>
<testscores count="5">
<score>65</score>
<score>77</score>
<score>67</score>
<score>64</score>
<score>80</score>
</testscores>
</student>
</classroll>
回答by tenorsax
You're probably missing IsStartElement() condition in your while loop:
您可能在 while 循环中缺少 IsStartElement() 条件:
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.Name == "id")
{
id = reader.ReadString();
}
...
}
Also, it would be easier to use XPathor LINQ to XMLto read your XML, of course it depends on the file. Here are some examples: XPathand LINQ.
此外,使用XPath或LINQ to XML来读取 XML会更容易,当然这取决于文件。下面是一些示例:XPath和LINQ。
EDIT: after seeing XML file details
编辑:在看到 XML 文件详细信息后
You should update your logic to keep track of current studentand its testscores. Also, note that countis an attribute. It can get messy pretty soon, I suggest you take a look at the samples mentioned above.
您应该更新您的逻辑以跟踪当前student及其testscores. 另外,请注意这count是一个属性。它很快就会变得混乱,我建议您查看上面提到的示例。
回答by Mikhail Rassudishkin
I think, that you get best result using XmlDocument
我认为,使用 XmlDocument 可以获得最佳结果
public void ReadXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<name file>.xml");
xmlEntities = new List<XmlEntity>();
foreach(XmlNode item in xmlDoc.ChildNodes)
{
GetChildren(item);
}
}
private void GetChildren(XmlNode node)
{
if (node.LocalName == "Строка")
{
//<you get the element here and work with it>
}
else
{
foreach (XmlNode item in node.ChildNodes)
{
GetChildren(item);
}
}
}
回答by Jon Koivula
The reason its not working because, example: when reader.Name == "firstname" is true but its not true with its elements value. What it exactly means is reader object reads next Nodetype, which is XmlNodeType.Element. So in this case looking at your XML file, using reader.Read(); function again reads next node, which is XmlNodeType.Text, and its value is then Joe. Im givin you example of working version.
它不起作用的原因是,例如:当 reader.Name == "firstname" 为真但它的元素值不为真时。它的确切含义是 reader 对象读取下一个 Nodetype,即 XmlNodeType.Element。因此,在这种情况下,使用 reader.Read(); 查看您的 XML 文件。函数再次读取下一个节点,即 XmlNodeType.Text,其值为 Joe。我给你工作版本的例子。
void ReadXMLFile()
{
XmlTextReader reader = new XmlTextReader("ClassRoll.xml");
reader.Read();
while (reader.Read())
{
if (reader.Name == "id")
{
reader.Read();
if(reader.NodeType == XmlNodeType.Text)
{
id = reader.Value;
reader.Read();
}
}
}
}
}

