gpt4 book ai didi

C#:检查 XML 中的重复元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:28:51 25 4
gpt4 key购买 nike

我想检查 XML 中的重复元素。请帮忙!

XML:

<?xml version="1.0" encoding="utf-8"?>
<List>
<Email_Entry></Email_Entry>
<Email_Entry EmailID="qw@gmail.com" />
<Email_Entry EmailID="qw@gmail.com" />
<Email_Entry EmailID="qw@gmail.com" />
<Email_Entry EmailID="qw@gmail.com" />
<Email_Entry EmailID="shssh@gmail.com" />
</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";
}
}

最佳答案

检查属性值而不是元素:

var EmailExist = Emailxml.Descendants("Email_Entry")
.Any(x => x.Attribute("EmailID").Value == txtEmailSubscribe.Text);

关于C#:检查 XML 中的重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com