简体   繁体   English

使用C#从xml文件获取值

[英]Getting values from xml file using C#

I'm pretty new to xml and I don't know how to read/get values from xml file below: 我是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>

Sorry for the terrible formatting, can't get it right. 抱歉可怕的格式化,无法做到正确。

First of all, I tried to load this file in XDocument results in a "Non-white space cannot be added to content" exception, but it didn't occur if loaded into XmlDocument. 首先,我试图在XDocument结果中加载此文件,“非白色空间无法添加到内容”异常,但如果加载到XmlDocument中则不会发生。

My code to attempt to get the name value: 我的代码试图获取名称值:

        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());
        }

Sadly while debugging the nList has a count of zero and I cannot figure out why. 可悲的是,调试nList的计数为零,我无法弄清楚原因。 I've tried looking at a ton of questions already on here and tutorials elsewhere and I'm just getting frustrated. 我已经尝试过在这里看到很多问题和其他地方的教程,我只是感到沮丧。 How in the world do I get the values out of name and other nodes? 我如何从名称和其他节点中获取值? Can someone explain this? 有人可以解释一下吗? And perhaps why I get the non-white space error with XDocument? 也许为什么我用XDocument得到非空格错误?

doc.SelectNodes("/category/name")

You're not finding any nodes because 1) the first node is Jeopardy , not category and 2) name is an attribute of category not a child element. 您没有找到任何节点,因为1)第一个节点是Jeopardy ,而不是category ,2) name是category的属性而不是子元素。

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

Like this: 像这样:

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

Make sure that the encoding of the file matches the encoding expected by your document loading method. 确保文件的编码与文档加载方法所需的编码相匹配。 Usually UTF8 is the prefered encoding of XML files. 通常UTF8是XML文件的首选编码。

As noted above, you can use: 如上所述,您可以使用:

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

or 要么

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

or 要么

doc.SelectNodes("//name");

You need to open XML document 您需要打开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");

Once you get your node then you can do necessary thing with it 一旦你得到你的节点,你就可以用它做必要的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM