Input html data in xml

I need your help. I need to get data entered in webforms and convert to xml

any help would be very helpful for mine.

greetings for chile!


example:

I have a contact form that contains comments by email, etc., what I need is that when sending all this information is stored in an XML file as follows:

<form> <name>Felipe Avila</name> <email> favila@domain.com </email> etc </form> 

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

+4
source share
3 answers

Generally, you must first determine the structure of the XML document that you want to create. This will greatly simplify writing code to fill out the specified document. However, if you want to create something really common to create an XML document for any web form, here is how you could do it:

 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 this:

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

My two cents [and it's nowhere near to completion]
Go through the document tree ... or childNodes of your form. Add text to the line as you go. This will include recursion.

0
source

You can fill any text in XML <![CDATA[]]> without any problems.

Not very useful, although you cannot use XPath or XSLT for the data in this section.

You need to specify more detailed information in your question - how is text entered, what XML format do you need to produce? For what?

Update: (next comment)

Use the XML namespace in .NET — specifically, the XmlDocument class — is Save , which allows you to specify the file name to save (although you should look at other overloads on it).

You can add elements in the same way as the link that you put 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); 
0
source

Source: https://habr.com/ru/post/1301961/


All Articles