开发者

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

what is the easiest way to c开发者_开发技巧reate a structured xml file based on asp.net mvc 2 webform data ? i am looking for a c# 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.Deserializemethods 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜