简体   繁体   中英

create xml from webform in asp.net mvc 2 c#

what is the easiest way to create a structured xml file based on asp.net mvc 2 webform data ? i am looking for ac# solution and to use maybe linq/lambda expressions?

You can deserialize your model to Xml in your controller method ActionResult SaveMyObject(MySerializableClass myObject){ //do stuff here }

You should be able to use one of the overloaded XmlSerializer.Deserialize methods found within the System.Xml.Serialization namespace.

This is one example, found on the MSDN Documentation site

MySerializableClass myObject;

// Construct an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(MySerializableClass));

// To read the file, create a FileStream.
FileStream myFileStream = new FileStream("myFileName.xml", FileMode.Open);

// Call the Deserialize method and cast to the object type.
myObject = (MySerializableClass) 
mySerializer.Deserialize(myFileStream)

Your object will then be deserialized into the file. You could also use the MemoryStream class to deserialize it into a string variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM