本文介绍了我想检查XML中的重复元素,并在标签上为重复元素提供警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 XML: - <?xml version =1.0encoding =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: - < pre> 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已存在; } 其他 { Emailxml.Root.Add(新XElement(Email_Entry,新XAttribute(EmailID,EmailID)) ); Emailxml.Save(FILE_PATH); lblEmail.Text =订阅成功; } } 任何人都可以说错了吗?解决方案 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>What I have tried:aspx.cs:-<pre>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"; } }can anybody tell whts wrong? 解决方案 这篇关于我想检查XML中的重复元素,并在标签上为重复元素提供警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 13:20