Reading XML file using XDocument(C#3.0)
How to read the CountryName and CurrencyName from the following XML using XDocument
<CountryCurrencyMapping>
<MappingLayer CountryName ="US" CurrencyName="Dollar"></MappingLayer>
<MappingLayer CountryName ="UK" CurrencyName=开发者_Python百科"Pound"></MappingLayer>
<MappingLayer CountryName ="Argentina" CurrencyName="Peso"></MappingLayer>
</CountryCurrencyMapping>
The desired output being
CountryName : US CurrencyName:Dollar
CountryName : UK CurrencyName:Pound
CountryName : Argentina CurrencyName:Peso
I am using C#3.0 and dotnet framework 3.5
Thanks
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");
}
I got the answer
foreach (XElement element in doc.Root.Nodes())
{
string h1 = element.Attribute("CountryName").Value;
string h2 = element.Attribute("CurrencyName").Value;
}
Thanks
精彩评论