2013-06-30 75 views
1

我一直在嘗試過去的幾個小時,幾乎沒有成功,從.xml文件讀取。在C#中讀取.XML文件

我想:

XmlReader reader = XmlReader.Create("ChampionList.xml"); 

     reader.ReadToFollowing("Name"); 
     reader.MoveToFirstAttribute(); 
     string nume = reader.Value; 
     MessageBox.Show(nume); 

我的XML看起來是這樣的:

<?xml version="1.0" encoding="utf-8" ?> 
<main> 
    <Champion> 
    <Name>Aatrox</Name> 
    <Counter>Soraka</Counter> 
    </Champion> 
    <Champion>  
    <Name>Ahri</Name> 
    <Counter>Diana</Counter>  
    </Champion> 
</main> 

我想,每當我按下按鈕,讀取名稱和計數器。每次新的一個(第一個按鈕 - 第一個冠軍等)。

有人可以幫助我嗎?另外,對代碼的一點解釋會很好,如果有很多循環和東西,我仍然有很多東西需要學習。

+0

感謝您編輯代碼:) – Xzenon

+0

爲什麼要使用XmlReader?嘗試LINQ to XML(XDocument)。 –

回答

1

您的內容可能會發現使用比XmlReader更高級別的接口更容易。例如,你可以在Linq的如下這樣對XML:

// read in the entire document 
var document = XDocument.Load("ChampionsList.xml"); 

// parse out the relevant information 
// start with all "Champion" nodes 
var champs = documents.Descendants("Champion") 
    // for each one, select name as the value of the child element Name node 
    // and counter as the value of the child element Counter node 
    .Select(e => new { name = e.Element("Name").Value, counter = e.Element("Counter").Value }); 

// now champs is a list of C# objects with properties name and value 

foreach (var champ in champs) { 
    // do something with champ (e. g. MessageBox.Show) 
} 
+0

更改'後代(「Name」)。Single()'到'元素(「名稱」),我會上投票,並且也''計數器'。 –

+0

@ChuckSavage很好的通話。我做了更改 – ChaseMedallion

+0

非常感謝,它的工作:)後裔之間有什麼區別()Single()和Element()? – Xzenon

1

爲了測試XML有效性,我發現將文件的擴展名設置爲.XML然後將其放到Internet Explorer窗口上非常簡單。 Internet Explorer內置了一個相當不錯的XML查看器,它會讓你知道是否有錯誤。

(編輯:刪除有關呈現XML是無效的具體建議 - 這似乎已被標記方面的問題引起的。)

+0

你確定你的說法嗎? – rene

+0

當我回答時,XML看起來不同。除了在Internet Explorer中檢查有效性的提示之外,現在這個答案還沒有那麼有用。 – StilesCrisis

+0

我只確保根標籤出現...我沒有添加或刪除任何標籤...您的回答讓我開始編輯問題... – rene

1

使用ReadElementContentAsString獲得元素

XmlReader reader = XmlReader.Create("ChampionList.xml"); 

reader.ReadToFollowing("Name"); // read until element named Name 
string nume = reader.ReadElementContentAsString(); // read its content 
MessageBox.Show(nume); 
0

你爲什麼不讀他們列出一次,當過按下按鈕只是從你的列表中冒了出來。 XmlTextReader reader = new XmlTextReader(「yourfile.xml」);

  string elementName = ""; 

      List<string[]> Champion = new List<string[]>(); 
      string name = "";    

      while (reader.Read()) // go throw the xml file 
      { 

       if (reader.NodeType == XmlNodeType.Element) //get element from xml file 
       { 

        elementName = reader.Name; 
       } 
       else 
       { 

        if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue)) //fet the value of element 
        { 
         switch (elementName) // switch on element name weather Name or Counter 
         { 
          case "Name": 
           name = reader.Value; 
           break; 
          case "Counter": 
           string[] value = new string[] { name, reader.Value }; //store result to list of array of string 
           Champion.Add(value); 
           break; 

         } 
        } 
       } 
      }