簡體   English   中英

在C#中讀取.XML文件

[英]Reading an .XML file in 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>

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

有人能幫我嗎? 此外,對代碼的一些解釋會很好,如果有很多循環和東西,我還有很多東西需要學習。

為了測試XML的有效性,我發現將文件的擴展名設置為.XML然后將其放到Internet Explorer窗口中非常容易。 Internet Explorer內置了一個非常好的XML查看器,它會告訴您是否有錯誤。

(編輯:刪除了有關呈現的XML無效的特定建議 - 這似乎是由標記問題引起的。)

使用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);

您可能會發現使用比XmlReader更高級別的界面更容易。 例如,您可以在Linq to 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)
}

你為什么不把它們列入列表中,只要按下按鈕就可以從列表中取出。 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;

                        }
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM