Getting values from xml file using C#

孤街醉人 提交于 2019-12-11 04:09:42

问题


I'm pretty new to xml and I don't know how to read/get values from xml file below:

<?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.

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. 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?


回答1:


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.

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

Like this:

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



回答2:


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.

As noted above, you can use:

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

or

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

or

doc.SelectNodes("//name");



回答3:


You need to open XML document

 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



来源:https://stackoverflow.com/questions/14207811/getting-values-from-xml-file-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!