我有以下XML文件:XmlTextReader的問題
<?xml version="1.0"?><!--This document contains the profiles that have been created.--><Profiles>
<Profile>
<name>One</name>
<date>Two</date>
</Profile>
<Profile>
<name>One</name>
<date>Two</date>
</Profile>
<Profile>
<name>One</name>
<date>Two</date>
</Profile>
</Profiles>
的問題是,當我使用XmlTextReader的,它只能讀取第一規格,而忽略第二和第三位。
public ArrayList ReadProfiles() {
ArrayList result = new ArrayList();
Hashtable currentProfile = null;
string currentName = "";
string currentValue = "";
XmlTextReader textReader = new XmlTextReader(profilesPath);
// Read until end of file
while (textReader.Read()) {
switch(textReader.NodeType) {
case XmlNodeType.Text: {
currentValue = textReader.Value;
Debug.Log("found text = " + currentValue);
}
break;
case XmlNodeType.Element: {
currentName = textReader.Name;
switch(currentName) {
case "Profiles":
Debug.Log("found profiles");
break;
case "Profile":
Debug.Log("found profile");
break;
case "name":
Debug.Log("found name");
break;
case "date":
Debug.Log ("found date");
break;
default:
Debug.Log("default in");
break;
}
}
break;
case XmlNodeType.Comment:
Debug.Log("found comment");
break;
case XmlNodeType.EndElement:
Debug.Log("found end element" + textReader.Name.ToString());
break;
default:
Debug.Log("default out");
break;
}
}
textReader.Close();
return result;
}
,所以我得到:從我的測試用完全相同的代碼和數據 alt text http://www.freeimagehosting.net/uploads/deee5af3f3.jpg
我看不到與您所看到的相同的行爲。我複製了代碼,做了一個小改變Debug.Log - > Console.WriteLine,我發現配置文件正在被讀取三次。你確定你正在閱讀你認爲你正在閱讀的文件嗎?嘗試使用while語句頂部的textReader.ReadOuterXml()來查看您正在閱讀的文件中的內容。 – btlog 2011-01-24 22:48:28