C# 验证XML中的重复值
下面的代码是在Xml文件中附加C# 验证XML中的重复值,c#,xml,winforms,C#,Xml,Winforms,下面的代码是在Xml文件中附加customerRecord的函数。 我这里有两个问题: 1.追加记录时,如何检查XML中是否已经存在id和Mobile的值??? 2.如何通过id或Mobile搜索客户,并在文本框中显示值 private const string FileName = @"C:\test\Person.xml"; private void button1_Click(object sender, EventArgs e) { var xmlDoc = new X
customerRecord
的函数。
我这里有两个问题:
1.追加记录时,如何检查XML中是否已经存在id
和Mobile
的值???2.如何通过
id
或Mobile
搜索客户,并在文本框中显示值
private const string FileName = @"C:\test\Person.xml";
private void button1_Click(object sender, EventArgs e)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(FileName);
var subRoot = xmlDoc.CreateElement("Customer");
subRoot.SetAttribute("id", textBox6.Text.Trim());
var firstName = xmlDoc.CreateElement("FirstName");
var xmlTextUserName = xmlDoc.CreateTextNode(textBox1.Text.Trim());
firstName.AppendChild(xmlTextUserName);
subRoot.AppendChild(firstName);
if (xmlDoc.DocumentElement != null)
xmlDoc.DocumentElement.AppendChild(subRoot);
var email = xmlDoc.CreateElement("LastName");
var xmlTextEmail = xmlDoc.CreateTextNode(textBox2.Text.Trim());
email.AppendChild(xmlTextEmail);
subRoot.AppendChild(email);
if (xmlDoc.DocumentElement != null)
xmlDoc.DocumentElement.AppendChild(subRoot);
var mobile = xmlDoc.CreateElement("Mobile");
var xmlTextMobile = xmlDoc.CreateTextNode(textBox3.Text.Trim());
mobile.AppendChild(xmlTextMobile);
subRoot.AppendChild(mobile);
if (xmlDoc.DocumentElement != null)
xmlDoc.DocumentElement.AppendChild(subRoot);
var address = xmlDoc.CreateElement("Address");
var xmlTextAddress = xmlDoc.CreateTextNode(textBox4.Text.Trim());
address.AppendChild(xmlTextAddress);
subRoot.AppendChild(address);
if (xmlDoc.DocumentElement != null)
xmlDoc.DocumentElement.AppendChild(subRoot);
var country= xmlDoc.CreateElement("Country");
var xmlTextCountry = xmlDoc.CreateTextNode(textBox5.Text.Trim());
country.AppendChild(xmlTextCountry);
subRoot.AppendChild(country);
if (xmlDoc.DocumentElement != null)
xmlDoc.DocumentElement.AppendChild(subRoot);
xmlDoc.Save(FileName);
if (File.Exists(FileName))
return;
var textWritter = new XmlTextWriter(FileName, null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("CustomerRecord");
textWritter.WriteEndElement();
textWritter.Close();
}
//Search by id or by Mobile
private void button3_Click(object sender, EventArgs e)
{
}
示例XML:
<CustomerRecord>
<Customer id="6786">
<FirstName>khkjh</FirstName>
<LastName>jkhjkh</LastName>
<Mobile>887897</Mobile>
<Address>jk</Address>
<Country>fdgfdg</Country>
</Customer>
</CustomerRecord>
khkjh
jkhjkh
887897
jk
fdgfdg
解析(读取/搜索/操作)XML文档的方法有很多(数据集/数据表、DOM、XmlSerialization),但我建议使用LINQ-XML
XDocument doc = XDocument.Load(file);
string id="21";
XElement element = doc.Descendants("Customer")
.Where(p => p.Attribute("id").Value == id).FirstOrDefault();
if (element != null)
{
//found
string firstName = (string)element.Element("FirstName");
string mobile = (string)element.Element("Mobile");
}
else
{
//Not found
//To add a customer
var ele = new XElement("Customer");
ele.SetAttributeValue("id", id);
ele.Add(new XElement("FirstName", firstName));
ele.Add(new XElement("Mobile",mobile));
doc.Root.Add(ele);
doc.Save(file);
}
您可以创建一个类似于XML模式本身的对象,然后将该对象用于其他操作
从xml架构创建对象-
例如,从底部开始
// Create a Customer class.
public class Customer
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Mobile {get; set;}
public string Address {get; set;}
public string Country {get; set;}
}
// Create a CustomerRecord class.
public class CustomerRecord
{
public Customer customer {get; set;}
public int Id {get; set;}
}
// Create a collection of CustomerRecords
public List<CustomerRecord> custRecords {get; set;}
//创建一个客户类。
公共类客户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串Mobile{get;set;}
公共字符串地址{get;set;}
公共字符串国家{get;set;}
}
//创建CustomerRecord类。
公共类客户记录
{
公共客户客户{get;set;}
公共int Id{get;set;}
}
//创建CustomerRecords的集合
公共列表custRecords{get;set;}
接下来需要做的事情是遍历XML并将每个值添加到对象中。
最好将xml作为一个对象放在手中,因为您永远不知道将来需要做什么样的操作
如果您现在需要执行一些验证或搜索操作,您可以轻松地在列表对象本身上执行这些操作。:您能给我一些建议来解决我的问题吗??