XmlTextReader问题

时间:2011-01-24 22:34:46

标签: c# xml unity3d xmltextreader

我有以下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;
 }

所以我得到: alt text http://www.freeimagehosting.net/uploads/deee5af3f3.jpg

2 个答案:

答案 0 :(得分:0)

我的测试输出完全相同的代码和数据。 用Writeline替换Debug.Log。

default out
found comment
found profiles
default out
found profile
default out
found name
found text = One
found end elementname
default out
found date
found text = Two
found end elementdate
default out
found end elementProfile
default out
found profile
default out
found name
found text = One
found end elementname
default out
found date
found text = Two
found end elementdate
default out
found end elementProfile
default out
found profile
default out
found name
found text = One
found end elementname
default out
found date
found text = Two
found end elementdate
default out
found end elementProfile
default out
found end elementProfiles
default out

答案 1 :(得分:0)

这不是有效的XML。 XML规范只允许一个根节点(处理指令不算作节点),输入流包含多个根节点。如果你通过验证器把它放在那里就会扒手。