【问题标题】:c# Remove XML Tags in text boxesc# 删除文本框中的 XML 标签
【发布时间】:2014-12-02 20:49:29
【问题描述】:

我已经设法将 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

【问题讨论】:

    标签: c# xml tags


    【解决方案1】:

    例如,原来我在 customer.elements 末尾缺少 .value;

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

    希望这对其他人有所帮助

    【讨论】:

      【解决方案2】:

      你想选择元素的值而不是元素

      【讨论】:

        猜你喜欢
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 2021-04-26
        • 2018-02-10
        • 2018-05-22
        • 2020-06-16
        • 1970-01-01
        • 2022-10-24
        相关资源
        最近更新 更多