0

下面的代码是customerRecord在 XML 文件中追加 a 的函数。我这里有两个问题:

1.追加记录时,如何检查xml中是否已经存在idand的值??2.如何按或按 客户搜索并显示??Mobile
idMobiletextboxes

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>
4

2 回答 2

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);
 }
于 2012-08-09T07:14:33.263 回答
0

您可以创建一个类似于 XML 模式本身的对象,然后将该对象用于其他操作。

从 xml 模式创建对象 -如何将 XML 映射到 C# 对象

例如,从底部开始;

// 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 对象本身上做。

于 2012-08-09T07:35:13.407 回答