我是一個以C#和Web服務爲開端的初學者程序員。使用XmlTextReader
在Service.cs
文件我的web服務的,我創建了一個ReadXMLFile()
方法,我想讀現有的XML文件,從中獲取數據,並將其放置到我的IService.cs
創建相應的屬性(數據成員)文件。
我的問題是,我的代碼基本上沒有做任何事情。我試過尋找網站和教程,但真的沒有太多,特別是像我這樣的初學者。任何人都知道我應該如何去做這件事,因爲我迄今爲止的嘗試顯然是錯誤的。
以下是我的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();
}
}
}
這是我的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>
您可能會發現[最佳做法來解析XML文件(http://stackoverflow.com/q/55828/1048330)有用 – tenorsax 2012-04-14 03:43:50
你應該分享您的XML文件或它的一個樣本如果它太大,我們可以看到它的結構。 – 2012-04-14 04:06:00
你不應該直接使用XmlTextReader。改用'XmlReader.Create()'。 – 2012-04-14 05:19:52