2
我想檢查XML中的重複元素。請幫忙!C#:檢查XML中的重複元素
XML:
<?xml version="1.0" encoding="utf-8"?>
<List>
<Email_Entry></Email_Entry>
<Email_Entry EmailID="[email protected]" />
<Email_Entry EmailID="[email protected]" />
<Email_Entry EmailID="[email protected]" />
<Email_Entry EmailID="[email protected]" />
<Email_Entry EmailID="[email protected]" />
</List>
aspx.cs:
protected void btnEmailSubscribe_Click(object sender, EventArgs e)
{
string EmailID = txtEmailSubscribe.Text;
string FILE_PATH = Server.MapPath("~/Logs/SubscribedEmails.xml");
XDocument Emailxml = XDocument.Load(FILE_PATH);
var EmailExist = Emailxml.Descendants("Email_Entry")
.Any(x => (string)x.Element("EmailID") == txtEmailSubscribe.Text);
if (EmailExist)
{
lblEmail.Text = "EmailID already exist";
}
else
{
Emailxml.Root.Add(new XElement("Email_Entry",
new XAttribute("EmailID", EmailID)
));
Emailxml.Save(FILE_PATH);
lblEmail.Text = "Subscribed Successfully";
}
}
應該在哪裏而不是任何。任何將返回true/false, – jdweng
@jdweng當我檢查電子郵件存在我使用'任何' – Backs
@Backs謝謝你的幫助!!!!!!! –