-1
我已經設法將xml導入到文本框或富文本框,並從文本框導出爲.xml文件,但從.xml文件導入到文本框時,它不僅複製了內部的數據標籤以及標籤。有沒有辦法去除這個?c#刪除文本框中的XML標記
下面的代碼:
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");
}
這裏的最終結果是:
非常感謝, 10gez10