2011-01-24 5 views
1

I次のXMLファイルを持っている:XmlTextReaderは問題

<?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 http://www.freeimagehosting.net/uploads/deee5af3f3.jpg

+1

私はあなたが見ているのと同じ動作をしません。 Debug.Log - > Console.WriteLineという小さな変更を加えてコードをコピーしたところ、プロファイルが3回読み込まれているのがわかりました。あなたが読んでいると思うファイルを読んでいると確信していますか? while文の先頭にあるtextReader.ReadOuterXml()を試して、読み込んでいるファイルの内容を正確に確認してください。 – btlog

答えて

0

出力を。 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 
+0

ありがとう!、私はDebug.Log()が非常にゆっくりと各結果を印刷することを発見しました。私はConsole.WriteLine()を使用することができませんでしたが、代わりに同じ文字列のすべての結果を連結し、最後に解析が完了しました。 – chuckSaldana

0

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