下面的代码是在Xml文件中附加customerRecord
的功能。
我在这里有两个问题:
1.当我追加记录时,如何检查id
和Mobile
的值是否已存在于XML中?
2.如何通过id
或Mobile
搜索客户,并在textboxes
??
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>
答案 0 :(得分:2)
有许多方法(DataSet / DataTable,DOM,XmlSerialization)来解析(读取/搜索/操作)XML文档,但我想建议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);
}
答案 1 :(得分:0)
您可以创建一个类似于XML模式本身的对象,然后将该对象用于其他操作。
从xml架构创建对象 - How do I map XML to C# objects
例如,从底部开始;
// 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;}
您需要做的下一件事是,遍历XML并将每个值添加到对象中。 将xml保存为手中的对象总是更好,因为你永远不知道将来需要做什么样的操作。
如果您现在需要进行一些验证或搜索操作,可以在List对象本身轻松完成。