tags:

views:

48

answers:

3

Hi everyone!

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>[email protected]</email>
    etc
</form>

something like this .. http://xmlfiles.com/articles/michael/htmlxml/default.asp

A: 

My two cents [and this is nowhere near complete]
Traverse the document tree... or the childNodes of your form. Append text to a string as you go.
This will involve recursion.

ItzWarty
Hi itzWatry! first of all thanks for your help.what you say implies somehow read the file that is generated. aspx.desing?thanks again!
Felipe Avila
A: 

You can stuff any text into a XML <![CDATA[]]> section without a problem.

Not very useful though, as you can't use XPath or XSLT on the data in this section.

You need to specify more details in your question - how is the text entered, what format of XML you need to produce? For what use?

Update: (following comment)

Use the XML namespace in .NET - specifically the XmlDocument class - this has a Save method that will allow you to specify a filename to save to (though it is worth looking at the other overloads to it).

You can add elements in a similar fashion to the link you have supplied in your question, using the AppendChild methods.

Something like the following:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("form"));

XmlElement nameElement = xmlDoc.CreateElement("name");
nameElement.InnerText = nameCtrl.Text;
xmlDoc.DocumentElement.AppendChild(nameElement);

XmlElement emailElement = xmlDoc.CreateElement("email");
emailElement.InnerText = emailCtrl.Text;
xmlDoc.DocumentElement.AppendChild(emailElement);
Oded
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> [email protected] </ email>.....etc</ form>
Felipe Avila
XML file - on the disk? In memory?
Oded
+1  A: 

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