c# - Finding and Filtering XML Data -
i have xml document here:
<?xml version="1.0" encoding="utf-8" ?> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>hide heart</title> <artist>bonnie tyler</artist> <country>uk</country> <company>cbs records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>test title 1</title> <artist>test name 1</artist> <country>test country 1</country> <company>test company 1</company> <price>100.00</price> <year>1985</year> </cd> <cd> <title>test title 3</title> <artist>test name 3</artist> <country>test country 3</country> <company>test company 3</company> <price>1.99</price> <year>1984</year> </cd> <cd> <title>test title 2</title> <artist>test name 2</artist> <country>test country 2</country> <company>test company 2</company> <price>19.99</price> <year>1985</year> </cd> </catalog>
what trying find cds have year of 1985. i'm new linq xml , have no idea i'm doing. because internet resources specific example, i'm having trouble applying example.
here's c# i've coded far:
namespace readingxml { class program { static void main(string[] args) { xelement xelement = xelement.load("..\\..\\music.xml"); ienumerable<xelement> music = xelement.elements(); /*// read entire xml foreach (var item in catalogues) { console.writeline(item); } console.readline();*/ var query = item in music.descendants("cd") select new { year = item.element("year").equals(1985) }; foreach (var item in query) console.writeline(item.tostring()); console.readline(); } } }
can please tell me how can achieve i'm trying and/or why code isn't functioning?
xdocument x = xdocument.load(@"xmlfilelocation"); var cdsin1985 = x.element("catalog").elements("cd").where(e => e.element("year").value == "1985"); foreach (var item in cdsin1985) { console.writeline(string.format("title : {0}, artist : {1}", item.element("title").value, item.element("artist").value)); }
Comments
Post a Comment