XmlTextReader 忽略第二个和第三个 Profile 元素
我有以下 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;
}
I have the following XML file:
<?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>
The problem is that when I use XmlTextReader, it only reads the first profile and ignore the second and third.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的测试输出具有完全相同的代码和数据。
将 Debug.Log 替换为 Writeline。
Output from my test with exactly same code and data.
Replace Debug.Log with Writeline.
这不是有效的 XML。 XML 规范只允许有一个根节点(处理指令不算作节点),并且您的输入流包含多个根节点。如果你把它通过验证器,它就会呕吐。
That's not valid XML. Only one root node is permitted by the XML specification (processing instructions don't count as nodes) and your input stream contains multiple root nodes. If you put that through a validator it will barf.