Search code examples
c#asp.netxmltextboxxmldocument

save values from textbox to xml document


I want to save my webpage in XML format. I thought of using XmlDocument to save the values. I tried searching it but I couldn't find a proper way for saving the data entered in a textbox to the xml document.

Is there any way? Although incorrect, but this is what I've done till now.

 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);

Solution

  • You can try with - based on InnerText property

    // 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);