How to properly search xml document using LINQ C# -
hey guys , gals having hard time time figuring out how search xml document. have been reading other forms crazy today can't seem understand it. hopeing give me little more detailed information on how correct way , why using linq. here xml file.
<?xml version="1.0" encoding="utf-8"?> <body> <customers> <client> <firstname value="someguy" /> <lastname value="test" /> <phonenumber value="541555555" /> <address value="55 nowhere" /> <city value="sometown" /> <state value="somestate" /> </client> </customers> </body>
what tyring accomplish return of values of each element matches name of customer. here code.
ienumerable<xelement> test = doc.root.descendants() .where(nodename => nodename.name == "client" && nodename.descendants().any(x => x.name == "firstname" && x.value == "someguy")); foreach (xelement m in test) { messagebox.show(m.tostring()); }
would appreciate help. please if possible explain idea of using linq format if will. not sure how explain asking part more understanding of way works or format etc...
edit
i have tried solution given , still nothing seeming work. please show me doing wrong here.
private void button2_click(object sender, eventargs e) { string seach = txtsearch.text; xdocument doc = xdocument.load(@"c:\users\tim\desktop\test.xml"); var result = doc.elements("customers") .elements("client") .where(x => x.elements("firstname") .where(c => c.attribute("value").value == "someguy") .any()) .tolist(); foreach (var m in result) { messagebox.show(m.tostring()); } }
edit:
okay have gotten work , output data looking for. can please tell me if there more efficent way of doing have posted below edit.
private void button2_click(object sender, eventargs e) { string seach = txtsearch.text; xdocument doc = xdocument.load(@"c:\users\tim\desktop\test.xml"); var result = (from clientnode in doc.root.descendants("client") name in clientnode.descendants("firstname") name.attribute("value").value == "someguy" select new { fname = clientnode.element("firstname").attribute("value").value, lname = clientnode.element("lastname").attribute("value").value, phone = clientnode.element("phonenumber").attribute("value").value, address = clientnode.element("address").attribute("value").value, city = clientnode.element("city").attribute("value").value, state = clientnode.element("state").attribute("value").value }); foreach (var m in result) { messagebox.show(m.fname + "\n" + m.lname + "\n" + m.phone + "\n" + m.address + "\n" + m.city + "\n" + m.state); }
you can use xelement
, search using linq query these:
xelement doc = xelement.parse(xml); var result = doc.elements("customers") .elements("client") .where(x => x.elements("firstname") .where(c => c.attribute("value").value == "someguy") .any()) .tolist();
so input:
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <body> <customers> <client> <firstname value=""someguy"" /> <lastname value=""some last name"" /> <phonenumber value=""123456"" /> <address value=""some where"" /> <city value=""some town"" /> <state value=""some state"" /> </client> <client> <firstname value=""someotherguy"" /> <lastname value=""some other last name"" /> <phonenumber value=""123456"" /> <address value=""some other where"" /> <city value=""some other town"" /> <state value=""some other state"" /> </client> </customers> </body>"; xelement doc = xelement.parse(xml); var result = doc.elements("customers") .elements("client") .where(x => x.elements("firstname") .where(c => c.attribute("value").value == "someguy") .any()) .tolist();
the result be:
<client> <firstname value=""someguy"" /> <lastname value=""some last name"" /> <phonenumber value=""123456"" /> <address value=""some where"" /> <city value=""some town"" /> <state value=""some state"" /> </client>
and can show values example:
messagebox.show(string.format("firstname: {0}\nlastname: {1}\nphonenumber: {2}\naddress: {3}\ncity: {4}\nstate: {5}", result[0].element("firstname").attribute("value").value, result[0].element("lastname").attribute("value").value, result[0].element("phonenumber").attribute("value").value, result[0].element("address").attribute("value").value, result[0].element("city").attribute("value").value, result[0].element("state").attribute("value").value));
note:
- if know result should contain 0 or 1 elements, can use
firstordefault()
instead oftolist()
; - element names case sensitive pay attention firstname example.
- you can use
xelement.load()
load file examplexelement doc = xelement.load(@"d:\file.xml");
- the query more fault tolerant if select elements way
.where(c => c.name.tostring().tolower() == "customers".tolower())
- the query more fault-tolerant if select attributes way
.where(c => c.attributes("value").where(a=>a.value == "someguy").any())
Comments
Post a Comment