This is the code I used.
这是我使用的代码。
private void button1_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load("XMLDatabase.xml");
var NumberExist = doc.Descendants("Users")
.Any(x => (string)x.Element("ID") == txtId.Text);
if (NumberExist)
{
MessageBox.Show("Number already exist");
}
}
My Xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<User Name="aa" Occupation="dd" Date_Of_Birth="123456" NIC="123123" ID="79461" />
<User Name="Ali Rasheed" Occupation="Student" Date_Of_Birth="111694" NIC="4550246607037" ID="12661" />
<User Name="Asif Rasheed" Occupation="Civil Engineer" Date_Of_Birth="241190" NIC="4550346603073" ID="90939" />
</Users>
1 个解决方案
#1
0
First, there is no ID
element in Users
, It belongs to User
.
首先,用户中没有ID元素,属于User。
Second, ID
is not an Element
, It's an Attribute
.
其次,ID不是元素,它是属性。
Therefore, you should change your code like this:
因此,您应该像这样更改代码:
var NumberExist = doc.Descendants("User")
.Any(x => (string)x.Attribute("ID") == txtId.Text);
#1
0
First, there is no ID
element in Users
, It belongs to User
.
首先,用户中没有ID元素,属于User。
Second, ID
is not an Element
, It's an Attribute
.
其次,ID不是元素,它是属性。
Therefore, you should change your code like this:
因此,您应该像这样更改代码:
var NumberExist = doc.Descendants("User")
.Any(x => (string)x.Attribute("ID") == txtId.Text);