Чтение XML-файла с использованием XDocument (C # 3.0) - PullRequest
1 голос
/ 27 апреля 2010

Как прочитать CountryName и CurrencyName из следующего XML с использованием XDocument

<CountryCurrencyMapping>    
    <MappingLayer CountryName ="US" CurrencyName="Dollar"></MappingLayer>
    <MappingLayer CountryName ="UK" CurrencyName="Pound"></MappingLayer>
    <MappingLayer CountryName ="Argentina" CurrencyName="Peso"></MappingLayer>    
  </CountryCurrencyMapping>

Желаемый результат:

CountryName : US CurrencyName:Dollar
CountryName : UK CurrencyName:Pound
CountryName : Argentina CurrencyName:Peso

Я использую C # 3.0 и dotnet framework 3.5

Спасибо

Ответы [ 2 ]

1 голос
/ 27 апреля 2010
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");
}
0 голосов
/ 27 апреля 2010

Я получил ответ

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

            } 

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...