【问题标题】:Getting values from xml file using C#使用 C# 从 xml 文件中获取值
【发布时间】:2013-01-08 03:33:47
【问题描述】:

我对 xml 很陌生,我不知道如何从下面的 xml 文件中读取/获取值:

<?xml version="1.0" encoding="utf-8" ?>
<Jeopardy>
  <category name = 'People in Computing'>
<first points = '100' answer = 'Alan Turing'>Known as the questioner of the human   mind, this man is known for helping tell humans and computers apart.</first>
<second points = '200' answer = 'Grace Hopper'>This female pioneer of the COBOL computer programming language was an Admiral in the US Navy.</second>
<third points = '300' answer = 'Tim Berners-Lee'>Called the father of the world wide web, this man is the director of the W3C.</third>
<fourth points = '400' answer = 'Lawrence Lessig'>An American academic and political activist who founded the Creative Commons, this man lobbies for reduced legal restrictions on copyrights and trademarks in the technology sector.</fourth>
<fifth points = '500' answer = 'Ada Lovelace'>This woman, known as the world's first computer programmer was also a Countess.</fifth>
  </category>
</Jeopardy>

抱歉,格式很糟糕,无法正确处理。

首先,我尝试在 XDocument 中加载此文件导致“无法将非空格添加到内容”异常,但如果加载到 XmlDocument 中则不会发生。

我尝试获取名称值的代码:

        string fileName = @"C:\Users\Kara\documents\visual studio 2010\Projects\Final Project\Final Project\Jeopardy.xml";

        XmlDocument doc = new XmlDocument();

        doc.Load(fileName);

        List<string> categories = new List<string>();

        XmlNodeList nList = doc.SelectNodes("/category/name");

        foreach (XmlNode node in nList)
        {
            categories.Add(node.ToString());
        }

可悲的是,在调试 nList 时计数为零,我不知道为什么。我已经尝试在这里查看大量问题和其他地方的教程,但我只是感到沮丧。我到底如何从名称和其他节点中获取值?有人可以解释一下吗?也许为什么我会收到 XDocument 的非空白错误?

【问题讨论】:

    标签: c# xml xmlreader


    【解决方案1】:

    doc.SelectNodes("/category/name")

    您没有找到任何节点,因为 1) 第一个节点是 Jeopardy,而不是 category 和 2) name 是类别的属性而不是子元素。

    试试:doc.SelectNodes("/Jeopardy/category/@name")

    像这样:

    foreach (XmlNode node in nList) {
      categories.Add(node.Value);
    }
    

    【讨论】:

    • 哦,非常感谢!那 (node.Attributes["name"].Value 就像一个魅力!
    • 我实际上编辑了答案以使其更简单一点,以便您直接选择属性。我认为现在更清晰,更接近你所拥有的。
    【解决方案2】:

    确保文件的编码与您的文档加载方法所期望的编码相匹配。通常 UTF8 是 XML 文件的首选编码。

    如上所述,您可以使用:

    doc.SelectNodes("/Jeopardy/category/name");
    

    doc.SelectNodes("//category/name");
    

    doc.SelectNodes("//name");
    

    【讨论】:

      【解决方案3】:

      您需要打开 XML 文档

       XmlDocument _document = new XmlDocument();
          byte[] bytes = File.ReadAllBytes(filePath);
         string xml = Encoding.UTF8.GetString(bytes);
          try
          {
          _document.LoadXml(xml);
          }
          catch (XmlException e)
          {
          //exception handling
          }                  
      
          var doc = (XmlDocument)_document.CloneNode(true);
      
          XmlNode node = doc.GetElementsByTagName("your child node name");
      

      一旦你得到你的节点,你就可以用它做必要的事情

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多