【问题标题】:save values from textbox to xml document将文本框中的值保存到 xml 文档
【发布时间】:2012-09-26 11:30:06
【问题描述】:

我想以 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);

【问题讨论】:

  • 您能否举例说明输入的数据和您预期的 XML 文件?
  • 一般信息中输入的数据是字母数字,xml没有特定的格式。
  • 应该有一些格式可以用来保存数据...
  • 那么如果你想要在文件中保存一个字符串,你为什么要使用XML呢?一个简单的文本文件还不够?
  • 它不仅仅是字符串。这只是第一个元素。我有一个适当的网页,其中包含下拉列表、单选按钮和文本字段。

标签: c# asp.net xml textbox xmldocument


【解决方案1】:

您可以尝试使用 - 基于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);

【讨论】:

  • 它正在工作,但输出未显示我在文本框中输入的内容。在一般信息中,它给了我这个System.Web.UI.WebControls.TextBox
  • Syrion 是的,我复制了您输入的代码,因为我不知道您的需要,但替换为 Text 属性,很高兴您解决了问题
  • 我很乐意帮助你 Syrion
【解决方案2】:

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

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

      )
    )
  );

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2017-09-09
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多