XmlTextReader ignora secondo e terzo elemento Profile
-
24-10-2019 - |
Domanda
Ho il seguente file 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>
Il problema è che quando uso XmlTextReader, si legge solo il primo profilo e ignorare la seconda e la terza.
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;
}
Soluzione
Uscita dal mio test con esattamente lo stesso codice e dati. Sostituire debug.log con 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
Altri suggerimenti
Questo non è XML valido. Un solo nodo radice è consentito dalla specifica XML (le istruzioni di elaborazione non contano come nodi) e il vostro flusso di input contiene più nodi principali. Se metti che attraverso un validatore sarà barf.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow