Reputation: 57
Here is my XML file
<Customer>
<PrivateCustomer>
<Adresse>USA</Adresse>
<Phone>12345678</Phone>
<Name>Zaghi</Name>
<Age>20</Age>
<Sex>Man</Sex>
</PrivateCustomer>
<PrivateCustomer>
<Adresse>USA</Adresse>
<Phone>12345678</Phone>
<Name>John</Name>
<Age>22</Age>
<Sex>Woman</Sex>
</PrivateCustomer>
</Customer>
I dont want to have duplicates Phone numbers. When i enter a phone number into a textbox i want it to check if the number already exist or no. If it exist there should come an error.
Here is a small part of my C# code:
XDocument doc = new XDocument();
doc = XDocument.Load("PrivateCustomer.xml");
var NumberExist = doc.Descendants("PrivateCustomer").Where(x => !x.Elements("Phone").Any());
if (NumberExist != null)
{
MessageBox.Show("Number already exist");
}
Upvotes: 0
Views: 791
Reputation: 101681
You can use Any
method:
XDocument doc = XDocument.Load("PrivateCustomer.xml");
var NumberExist = doc.Descendants("PrivateCustomer")
.Any(x => (string)x.Element("Phone") == textBox1.Text);
if(NumberExist)
{
MessageBox.Show("Number already exist");
}
If all PrivateCustomers
have at least one Phone
,your query will always return null.Just use a condition with Any
and check if there is a PrivateCustomer
with a given phone number.
Upvotes: 1