c#在文本框中删除XML标记

时间:2014-12-02 20:49:29

标签: c# xml tags

我已经设法将xml导入文本框或富文本框并从文本框导出到.xml文件,但是当从.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");

    }

这是最终结果:

http://imgur.com/ssMFJ3h

非常感谢, 10gez10

2 个答案:

答案 0 :(得分:0)

原来我在customer.elements结束时缺少.value,例如;

Title = customer.Element(" Title")。Value

希望这有助于其他人

答案 1 :(得分:0)

您想要选择元素的值而不是元素