문제

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;
 }

so I get: alt text

도움이 되었습니까?

해결책

Output from my test with exactly same code and data. Replace Debug.Log with Writeline.

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top