【发布时间】:2011-06-14 20:28:15
【问题描述】:
我有以下 XML 文件:
<?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;
}
【问题讨论】:
-
我没有看到与您看到的相同的行为。我复制了代码,做了一个小改动 Debug.Log -> Console.WriteLine,我看到配置文件被读取了三遍。你确定你正在阅读你认为你正在阅读的文件。在 while 语句的顶部尝试 textReader.ReadOuterXml() 以查看您正在阅读的文件中的确切内容。
标签: c# xml unity3d xmltextreader