Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:28:54+00:00 2026-06-09T07:28:54+00:00

Below code is the function to Append a customerRecord in the Xml file. I’ve

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:28:56+00:00Added an answer on June 9, 2026 at 7:28 am

    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);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i write below code for sending id to another php file function selectCheckBox(k) {
The below code doesn't it print the value. function go(x) { alert(x.options.selectedIndex.value); //location=document.menu.student.options[document.menu.student.selectedIndex].value }
In the code below: DbModels.prototype.updateById = function(_collection, id, field, value, callback) { this.getCollection(_collection, function(error,
I am getting an $(<div/>).text(value).html is not a function error in the code below:
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
So I have the below code: var formData = new FormData(); formData.append(title, document.getElementById(title).value); formData.append(html,my_html);
Hi I have written below code function unique(th){ var obj = {}; for(var i
Using the SQL in the below code: public function saveOrder() { $id = $this->getUserId();
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
In the below code how does passing bar as function (n) { return n;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.