質問

次の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を使用する場合、最初のプロファイルのみを読み取り、2番目と3番目のプロファイルを無視することです。

    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

役に立ちましたか?

解決

まったく同じコードとデータを使用したテストからの出力。 debug.logを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

他のヒント

それは有効なXMLではありません。 XML仕様(処理命令はノードとしてカウントされない)で許可されているルートノードは1つだけで、入力ストリームには複数のルートノードが含まれています。それをバリデーターに置くと、それはbarfになります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top