简体   繁体   English

使用C#中的节点值从XML读取数据

[英]Read data from XML using node values in C#

I know this question is asked so many times. 我知道这个问题被问过很多次了。 But I couldn't find a proper answer yet. 但是我找不到合适的答案。

I have an XML file with some country codes and country names in it. 我有一个带有一些国家代码和国家名称的XML文件。 Here it is. 这里是。

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <CountryCodeNames>
      <Country CountryID="0" EnglishCountryName="NOT DEFINED" ArabicCountryName="NOT DEFINED"/>
      <Country CountryID="001" EnglishCountryName="ALGERIA    " ArabicCountryName="الجزائر"/>
      <Country CountryID="002" EnglishCountryName="ANGOLA    " ArabicCountryName="       انجولا "/>
      <Country CountryID="003" EnglishCountryName="BOTSWANA     " ArabicCountryName="       بوتسوانا "/>
      <Country CountryID="004" EnglishCountryName="BURUNDI     " ArabicCountryName="    بوروندى "/>
      <Country CountryID="005" EnglishCountryName="CAMERON REPUBLIC     " ArabicCountryName="     جمهوريه الكمرون  "/>
      <Country CountryID="006" EnglishCountryName="CENTRAL AFRICAN REP.   " ArabicCountryName="جمهوريهافريقياالوسطي "/>
      <Country CountryID="007" EnglishCountryName="CHAD   " ArabicCountryName="     تشاد  "/>
      <Country CountryID="008" EnglishCountryName="CONGO (BRAZZAVILLE)    " ArabicCountryName="  )الكونغوا(برازافيل       "/>
      <Country CountryID="009" EnglishCountryName="CONGO (DRC)" ArabicCountryName="جمهورية الكنغوليس"/>
      <Country CountryID="010" EnglishCountryName="BENIN (PEOPLE REPUB)       " ArabicCountryName="جمهوريه بنين الشعبيه"/>
      <Country CountryID="011" EnglishCountryName="ETHIOPIA     " ArabicCountryName="     أثيوبيا "/>
      <Country CountryID="012" EnglishCountryName="GABON    " ArabicCountryName="     جمهوريه الجابون  "/>
      <Country CountryID="013" EnglishCountryName="GHANA   " ArabicCountryName="        غانا "/>
      <Country CountryID="014" EnglishCountryName="GUINEA       " ArabicCountryName="    غينيا "/>
</CountryCodeNames>

Now in my windows form I got the CountryID and I want to read EnglishCountryName from this XML file according to the CountryID I have. 现在在我的Windows窗体中,我得到了CountryID,我想根据我拥有的CountryID从此XML文件中读取EnglishCountryName。 This is so far I have tried. 到目前为止,我已经尝试过了。 Dont know much about XML file reading. 对XML文件读取了解不多。 Please help. 请帮忙。

string natxmlcode = crdv4.smartcardData.NationalityCode.ToString();
                XmlDocument xd = new XmlDocument();
                string xmlpath = @"D:\CountriesNameList.xml";
                xd.Load(xmlpath);
                string nationality = xd.SelectSingleNode("CountryCodeNames/CountryID="+natxmlcode+"/EnglishCountryName").InnerText;

'natxmlcode' is CountryID. “ natxmlcode”是CountryID。

I would suggest to use XDocument; 我建议使用XDocument; it is easier and cleaner to use than XmlDocument. 它比XmlDocument更容易使用。

To select the name, use this code: 要选择名称,请使用以下代码:

string natcode = crdv4.smartcardData.NationalityCode.ToString();
string xmlpath = @"D:\CountriesNameList.xml";
XDocument xd = XDocument.Load(xmlpath);

string nationality = xd.Descendants("Country")
       .FirstOrDefault(c => c.Attribute("CountryID").Value.Equals(natcode))
       .Attribute("EnglishCountryName").Value;

Note: you will need the System.Xml.Linq namespace. 注意:您将需要System.Xml.Linq命名空间。

Sebastian's answer really helped in VS 2013. But I wanted the same functionality in VS 2008 with .Net framework 2.0. Sebastian的回答确实对VS 2013有所帮助。但是我希望VS 2008中具有.Net framework 2.0的相同功能。 So this is what I did and working fine. 所以这就是我所做的并且工作正常。 Thanks to Sam Axe. 感谢Sam Axe。

string xmlpath = @"D:\CountriesNameList.xml";
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);
XmlNode node = xml.SelectSingleNode("//CountryCodeNames/Country[@CountryID="+natxmlcode+"]");
string nationality = node.Attributes[1].Value.ToString();

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

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