html data input to xml

c#, webforms, xml

Solution

Typically, you should define the structure of the XML document you want to produce first. That will make writing the code to populate said document much easier. However, if you're wanting something really generic to generate an XML document for any web form, here's how you could do so:

XmlDocument buildDocument(Control control)
{
    var xmlDoc = new XmlDocument();
    xmlDoc.AppendChild(xmlDoc.CreateElement("Form"));
    buildDocumentRecursive(xmlDoc, control);
    return xmlDoc;
}

void buildDocumentRecursive(XmlDocument xmlDoc, Control control)
{
    var textCtrl = control as IEditableTextControl;
    if (textCtrl != null)
    {
        var element = xmlDoc.CreateElement(control.ClientID);
        element.InnerText = textCtrl.Text;
        xmlDoc.DocumentElement.AppendChild(element);
    }
    // If you want to check for check boxes, radio buttons, etc., add other cases
    else
    {
        foreach (var child in control.Controls)
        {
            buildDocumentRecursive(xmlDoc, child);
        }
    }
}

Another way to do it:

var document = new XDocument(
    new XElement(
        "Fields",
        from field in Request.Form.AllKeys
        select new XElement(field, Request.Form[field])));        

Problem

I need your help. I need to get the data entered in a webforms and transform to xml any help will be very useful for my. greetings for chile! an example: I have a contact form that contains name email comments etc. .. what I need, is that when making the submit all this information is stored in an xml file like this: ``` <form> <name>Felipe Avila</name> <email>favila@domain.com</email> etc </form> ``` something like this .. http://xmlfiles.com/articles/michael/htmlxml/default.asp

Original source