我将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);
}
答案 0 :(得分:1)
您可以使用普通StringBuilder
。
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