C# 3.0 使用XDocument(C#3.0)读取XML文件

C# 3.0 使用XDocument(C#3.0)读取XML文件,c#-3.0,C# 3.0,如何使用XDocument从以下XML中读取CountryName和CurrencyName <CountryCurrencyMapping> <MappingLayer CountryName ="US" CurrencyName="Dollar"></MappingLayer> <MappingLayer CountryName ="UK" CurrencyName="Pound"></MappingLayer&g

如何使用XDocument从以下XML中读取CountryName和CurrencyName

<CountryCurrencyMapping>    
    <MappingLayer CountryName ="US" CurrencyName="Dollar"></MappingLayer>
    <MappingLayer CountryName ="UK" CurrencyName="Pound"></MappingLayer>
    <MappingLayer CountryName ="Argentina" CurrencyName="Peso"></MappingLayer>    
  </CountryCurrencyMapping>
我正在使用C#3.0和dotnetframework3.5

谢谢

我得到了答案

foreach (XElement element in doc.Root.Nodes())
            {
                string h1 = element.Attribute("CountryName").Value;
                string h2 = element.Attribute("CurrencyName").Value;

            } 
谢谢

我得到了答案

foreach (XElement element in doc.Root.Nodes())
            {
                string h1 = element.Attribute("CountryName").Value;
                string h2 = element.Attribute("CurrencyName").Value;

            } 
谢谢

XDocument xmldoc = XDocument.Parse(YourXmlString,LoadOptions.PreserveWhitespace);
XElement XCountryCurrency= xmldoc.Element("CountryCurrencyMapping");

StringBuilder sbCountry = new StringBuilder("");

foreach (var item in XCountryCurrency.Elements())
{
 sbCountry.Append("CountryName : " +  item.Attribute("CountryName").Value().ToString());
 sbCountry.Append("CurrencyName: " +  item.Attribute("CurrencyName").Value().ToString());
 sbCountry.Append("\n");
}