2011-01-24 16 views
1

我有以下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的,它只能讀取第一規格,而忽略第二和第三位。

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,我發現配置文件正在被讀取三次。你確定你正在閱讀你認爲你正在閱讀的文件嗎?嘗試使用while語句頂部的textReader.ReadOuterXml()來查看您正在閱讀的文件中的內容。 – btlog 2011-01-24 22:48:28

回答

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 
+0

謝謝!我發現Debug.Log()打印每個結果非常緩慢。我無法使用Console.WriteLine(),而是將每個結果連接在同一個字符串中,最後解析完成。 – chuckSaldana 2011-01-24 23:01:48

0

這不是有效的XML。 XML規範只允許有一個根節點(處理指令不算作節點),並且您的輸入流包含多個根節點。如果你通過驗證器,它會barf。