0

Below code is the function to Append a customerRecord in the Xml file. I've got 2 questions here:

1.When I append a record, how to check whether the value of id and Mobile is already existing in the XML??
2.How to search a customer by id or by Mobile and display the values in the 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)
{

}

Sample XML:

<CustomerRecord>
  <Customer id="6786">
    <FirstName>khkjh</FirstName>
    <LastName>jkhjkh</LastName>
    <Mobile>887897</Mobile>
    <Address>jk</Address>
    <Country>fdgfdg</Country>
  </Customer>
</CustomerRecord>
Sandeep
  • 5,581
  • 10
  • 42
  • 62
linguini
  • 1,939
  • 5
  • 49
  • 79

2 Answers2

2

There are number of ways (DataSet/DataTable, DOM,XmlSerialization) to parse (read/search/manipulate) the XML document but I'd like to suggest 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);
 }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

You can create an object similar to the XML schema itself, and then use the object for other operations.

Creating objects from xml schema - How do I map XML to C# objects

For example, starting from bottom;

// 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;}

The next thing you need to do is, Traverse the XML and add each value into the objects. It's always better to keep the xml as an object in hand, because you never know what kind of operations you will need to do in the future.

If you need to to do some validations or search operations at the moment, you can easily do them on the List object itself.

Community
  • 1
  • 1
Sandeep
  • 5,581
  • 10
  • 42
  • 62