将值从文本框保存到xml文档

时间:2012-09-26 11:30:07

标签: c# asp.net xml textbox xmldocument

我想以XML格式保存我的网页。我想过使用XmlDocument来保存值。我尝试搜索它但我找不到将文本框中输入的数据保存到xml文档的正确方法。

有什么办法吗?虽然不对,但这是我到目前为止所做的。

 XmlDocument XDoc = new XmlDocument();


        // Create root node.
        XmlElement XElemRoot = XDoc.CreateElement("Generate_License");

        //Add the node to the document.
        XDoc.AppendChild(XElemRoot);


        XmlElement Xsource = XDoc.CreateElement("General_Info", txtGInfo.ToString());
        XElemRoot.AppendChild(Xsource);

2 个答案:

答案 0 :(得分:0)

您可以尝试使用 - 基于InnerText属性

// Create the xml document containe
XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);// Create the root element

XmlElement root = doc.CreateElement("Generate_License");

XmlElement elem= doc.CreateElement("General_Info");
elem.InnerText =txtGInfo.Text;

root.AppendChild(elem);
doc.AppendChild(root);

答案 1 :(得分:0)

试试这个,这很简单。只是你需要4.0 .net框架

XDocument doc =
  new XDocument(
    new XElement("Generate_License",
      new XElement("General_Info", txtGInfo.ToString())

      )
    )
  );