我想检查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";
}
}
答案 0 :(得分:3)
检查属性值而不是元素:
var EmailExist = Emailxml.Descendants("Email_Entry")
.Any(x => x.Attribute("EmailID").Value == txtEmailSubscribe.Text);
答案 1 :(得分:0)
var xml = File.ReadAllText(&#34;您的xml文件路径&#34;);
public void processStartNotification() {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(locationActivity);
builder.setContentTitle("New Item Received")
.setAutoCancel(true)
.setContentText(title)//coming from server
.setSmallIcon(R.drawable.app_logo);
Intent mainIntent = new Intent(context,Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
PreferenceClass.setBooleanPreference(locationActivity,Constant.NOTIFICATION,true);
final NotificationManager manager = (NotificationManager) this.locationActivity.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}