[英]How to check whether the input from textbox available in the XML file or not? But no result is shown from my code
這是我使用的代碼。
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");
}
}
我的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>
首先 , Users
沒有ID
元素,它屬於User
。
其次 , ID
不是Element
,而是Attribute
。
因此,您應該像這樣更改代碼:
var NumberExist = doc.Descendants("User")
.Any(x => (string)x.Attribute("ID") == txtId.Text);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.