If you wanna fetch all the values from the xml file, you can try to do this:
class Program
{
static void Main(string[] args)
{
XDocument add = XDocument.Load("XMLFile1.xml");
var result = from item in add.Descendants("Valute")
select new
{
Name = item.Element("Name").Value,
Value = item.Element("Value").Value
};
foreach (var item in result)
{
Console.WriteLine(item.Name+"<===>"+item.Value);
}
}
}
If you only wanna fetch all the Name and Value from the "foreach" statement, you can just use "if" statement to decide which to deal with in the "foreach" loop statement.
cenk1536
Contributor
2503 Points
2119 Posts
How to Parse this?
Nov 02, 2012 08:19 AM|LINK
Hi,
I have to get the currency info from an XML. Here is the sample xml:
Best Regards.
Prashant Kum...
Star
12346 Points
1993 Posts
Re: How to Parse this?
Nov 02, 2012 09:49 AM|LINK
Try the following. It will give you an IEnumerable of anonymous objects with 2 properties each - Code and Rate
XElement elem = XElement.Parse(@"<ValCurs Date=""02.11.2012"" Name=""Test""> <ValType Type=""Xarici valyutalar""> <Valute Code=""KRW""> <Nominal>100</Nominal> <Name>Cənubi Korea vonu</Name> <Value>0.0719</Value> </Valute> <Valute Code=""RUB""> <Nominal>1</Nominal> <Name>Rusiya rublu</Name> <Value>0.0251</Value> </Valute> </ValType> </ValCurs>"); var arr = elem.Descendants().Elements("Valute").Select(x=> new { Code=x.Attribute("Code"), Rate=x.Element("Value").Value });cenk1536
Contributor
2503 Points
2119 Posts
Re: How to Parse this?
Nov 02, 2012 10:22 AM|LINK
Hi Prashant Kumar,
Thanks for your reply. How can I get specific ones??? Lets say there are 40 of them and I would like to get according to the Code.
anirudhagupt...
Member
185 Points
84 Posts
Re: How to Parse this?
Nov 02, 2012 11:25 AM|LINK
see this article http://www.dreamincode.net/forums/topic/48954-working-with-xml-files-in-c%23/#/
Prashant Kum...
Star
12346 Points
1993 Posts
Re: How to Parse this?
Nov 03, 2012 12:49 AM|LINK
Add a where clause to the linq query
var arr = elem.Descendants() .Elements("Valute") .Where(x => x.Attribute("Code").Value == "RUB") .Select(x => new { Code=x.Attribute("Code"), Rate=x.Element("Value").Value });Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to Parse this?
Nov 03, 2012 12:57 AM|LINK
Hi,
If you wanna fetch all the values from the xml file, you can try to do this:
class Program { static void Main(string[] args) { XDocument add = XDocument.Load("XMLFile1.xml"); var result = from item in add.Descendants("Valute") select new { Name = item.Element("Name").Value, Value = item.Element("Value").Value }; foreach (var item in result) { Console.WriteLine(item.Name+"<===>"+item.Value); } } }If you only wanna fetch all the Name and Value from the "foreach" statement, you can just use "if" statement to decide which to deal with in the "foreach" loop statement.