1

在过去的几个小时里,我一直在尝试从 .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>

每当我按下按钮时,我都想阅读名称和计数器。每次一个新的(第一个按钮按下 - 第一个冠军等等)。

有人能帮我吗?另外,对代码做一点解释会很好,如果有很多循环和东西,我还有很多东西要学。

4

4 回答 4

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);
于 2013-06-30T19:40:27.350 回答
1

您可能会发现使用更高级别的接口比使用 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)
}
于 2013-06-30T19:40:32.360 回答
1

为了测试 XML 的有效性,我发现将文件的扩展名设置为 .XML,然后将其放到 Internet Explorer 窗口中非常容易。Internet Explorer 内置了一个相当不错的 XML 查看器,如果有错误,它会通知您。

(编辑:删除了关于呈现的 XML 无效的具体建议——这似乎是由标记问题引起的。)

于 2013-06-30T19:25:33.200 回答
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;

                        }
                    }
                }
            }
于 2013-06-30T19:53:11.103 回答