2012-08-09 28 views
0

下面的代碼是在Xml文件中追加customerRecord的函數。 我在這裏有2個問題:驗證XML中的重複值

1.當我追加一條記錄時,如何檢查idMobile的值是否已經存在於XML?
2.如何通過idMobile搜索客戶,並在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> 

回答

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); 
} 
+0

:你能給我任何建議繼續我的問題? – linguini 2012-08-09 07:20:10

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對象本身上執行它們。