如何将Richtextbox中的字符串插入C#Winform中的XML文件中

时间:2017-07-18 11:18:49

标签: c# xml winforms savefiledialog

我将xml代码放入RichTextBox,用户可以在程序运行时进行更改。我希望在创建期间将RichTextBox内容保存在xml文件中。

注意: 我已经在创建一个xml文件。我希望在创建期间将RichTextBox的内容保存到xml文件中。

saveFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.ShowDialog();


if (saveFileDialog1.FileName != "")
{
    gridView1.AddNewRow();
    XmlDocument document = new XmlDocument();

    XmlElement anaeleman = document.CreateElement("onbase-certificate");

    //i want to do it here
    TextWriter yaz = new StreamWriter(saveFileDialog1.FileName,true);
    yaz.Write(f2.richTextBox1.Text);
    yaz.Close();

    XmlElement alteleman = document.CreateElement("licences");
    for (int i = 0; i < Convert.ToInt32(gridView1.DataRowCount); i++)
            {

                XmlElement elemanlar = document.CreateElement("licence");

                XmlElement eleman = document.CreateElement("name");
                eleman.InnerText = "![CDATA[" + gridView1.GetRowCellValue(i, "productname").ToString() + "]";
                elemanlar.AppendChild(eleman);

                eleman = document.CreateElement("code");
                eleman.SetAttribute("sig", "1");
                eleman.InnerText = gridView1.GetRowCellValue(i, "producttype").ToString();
                elemanlar.AppendChild(eleman);

                eleman = document.CreateElement("count");
                eleman.InnerText = gridView1.GetRowCellValue(i, "count").ToString();
                eleman.SetAttribute("sig", "1");
                elemanlar.AppendChild(eleman);

                if (gridView1.GetRowCellValue(i, "pilotdate").ToString() != "")
                {
                    eleman = document.CreateElement("pilotdate");
                    eleman.InnerText = gridView1.GetRowCellValue(i, "pilotdate").ToString();
                    eleman.SetAttribute("sig", "1");
                    elemanlar.AppendChild(eleman);
                }

                alteleman.AppendChild(elemanlar);

            }
    //anaeleman.AppendChild(alteleman);

    anaeleman.AppendChild(alteleman);

    document.AppendChild(anaeleman);

    document.Save(saveFileDialog1.FileName);
}

2 个答案:

答案 0 :(得分:1)

您可以使用普通StringBuilder

,而不是使用XmlDocument
var builder = new StringBuilder();
builder.AppendLine("<onbase-certificate>");
builder.AppendLine(f2.richTextBox1.Text);

builder.AppendLine("<licenses>");
for (int i = 0; i < Convert.ToInt32(gridView1.DataRowCount); i++)
        {

            builder.AppendLine("<license>");
            builder.AppendLine("<name>![CDATA[" + gridView1.GetRowCellValue(i, "productname").ToString() + "]</name>");

           builder.AppendLine("<code sig=\"1\">");
           builder.AppendLine(gridView1.GetRowCellValue(i, "count").ToString(););
           builder.AppendLine("</code>");

            // ...


           builder.AppendLine("</license>");

        }
builder.AppendLine("</licenses>");

builder.AppendLine("</onbase-certificate>");
File.WriteAllText(saveFileDialog1.FileName, builder.ToString());

答案 1 :(得分:0)

我不确定你想做什么。是否要将文本从richtextbox保存到xml元素anaeleman?如果是的话,你试过这个:?

anaeleman.InnerText = f2.richTextBox1.Text