c# Remove XML Tags in text boxes



I've managed to import xml to text boxes or rich text box and export to .xml file from text boxes though when importing from .xml file to text boxes it copies not only the data inside the tags but the tags as well. It there a way to remove this?


Here's the code:



private void btnimport_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.CheckFileExists = true;
open.InitialDirectory = "@C:\\";
open.Filter = "XML Files (*.xml)|*.xml|All Files(*.*)|*.*";
open.Multiselect = false;

if (open.ShowDialog() == DialogResult.OK)
{
try
{
XDocument doc = XDocument.Load(open.FileName);
var query = from customer in doc.Descendants("Customer")
select new
{
Title = customer.Element("Title"),
Firstname = customer.Element("FirstName"),
Lastname = customer.Element("LastName"),
DateofBirth = customer.Element("DateofBirth"),
Email = customer.Element("Email"),
HouseNo = customer.Element("HouseNo"),
Street = customer.Element("Street"),
Postcode = customer.Element("Postcode"),
Town = customer.Element("Town"),
County = customer.Element("County"),
ContactNo = customer.Element("ContactNo"),
};

txtxml.Text = "";
foreach (var customer in query)
{
txttitle.Text = txttitle.Text + customer.Title;
txtfname.Text = txtfname.Text + customer.Firstname;
txtlname.Text = txtlname.Text + customer.Lastname;
txtdob.Text = txtdob.Text + customer.DateofBirth;
txtemail.Text = txtemail.Text + customer.Email;
txthouseno.Text = txthouseno.Text + customer.HouseNo;
txtstreet.Text = txtstreet.Text + customer.Street;
txtpostcode.Text = txtpostcode.Text + customer.Postcode;
txttown.Text = txttown.Text + customer.Town;
txtcounty.Text = txtcounty.Text + customer.County;
txtcontactno.Text = txtcontactno.Text + customer.ContactNo;

txtxml.Text = txtxml.Text + customer.Title + "\n";
txtxml.Text = txtxml.Text + customer.Firstname + "\n";
txtxml.Text = txtxml.Text + customer.Lastname + "\n";
txtxml.Text = txtxml.Text + customer.DateofBirth + "\n";
txtxml.Text = txtxml.Text + customer.Email + "\n";
txtxml.Text = txtxml.Text + customer.HouseNo + "\n";
txtxml.Text = txtxml.Text + customer.Street + "\n";
txtxml.Text = txtxml.Text + customer.Postcode + "\n";
txtxml.Text = txtxml.Text + customer.Town + "\n";
txtxml.Text = txtxml.Text + customer.County + "\n";
txtxml.Text = txtxml.Text + customer.ContactNo + "\n";

MessageBox.Show("XML has been imported");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnexport_Click(object sender, EventArgs e)
{
XDocument doc = new XDocument(
new XElement("Booking",
new XElement("Customer",
new XElement("Title", txttitle.Text),
new XElement("FirstName", txtfname.Text),
new XElement("LastName", txtlname.Text),
new XElement("DateofBirth", txtdob.Text),
new XElement("Email", txtemail.Text),
new XElement("HouseNo", txthouseno.Text),
new XElement("Street", txtstreet.Text),
new XElement("Postcode", txtpostcode.Text),
new XElement("Town", txttown.Text),
new XElement("County", txtcounty.Text),
new XElement("ContactNo", txtcontactno.Text)
)));

doc.Save("Bookings.xml");
MessageBox.Show("XML has been saved");

}


Here's the end result:


http://ift.tt/11PZjyk


Many thanks, 10gez10


No comments:

Post a Comment