I have a hard time time figuring out how to properly search an xml document. I have been reading the other forms like crazy today but just can't seem to understand it. Was hopeing someone could give me a little more detailed information on how to do this the correct way and why using LINQ. Here is the 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 I am tyring to accomplish is to return all of the values of each element that matches by a name of a customer. Here is my 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 really appreciate the help. Please also if possible explain what the idea of using LINQ is like the format if you will. Not really sure how to explain what I am asking but for the most part just more understanding of the way it works or format etc...
EDIT
I have tried the solution given and still nothing is seeming to work. Please show me what I am 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 so I have gotten it to work now and output the data that I was looking for. Can someone please tell me if there is a more efficent way of doing what I have posted below this 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")
from name in clientNode.Descendants("Firstname")
where 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);
}
Clientnode if it has satisfyingFirstname.Value="someguy"condition?XElement doc = XElement.Parse(xml);