I want to serialize an object into an xml and I want the filename of the xml to be random as following
636211ad-ef28-47b9-aa60-207d3fbb9580.xml
fc3b491e5-59ac-4f6a-81e5-27e971b903ed.xml
I am just curious on how to do such thing?
I want to serialize an object into an xml and I want the filename of the xml to be random as following
636211ad-ef28-47b9-aa60-207d3fbb9580.xml
fc3b491e5-59ac-4f6a-81e5-27e971b903ed.xml
I am just curious on how to do such thing?
Good description of serialization with some encapsulation can be found here The name seems to be a Guid, so just create a new guid, convert it to text and use that as the filename.
var fileName = String.Format("{0}.xml", System.Guid.NewGuid().ToString());
Here is an example with a sample class.
public class TestSerialize
{
public string Test1;
public int Test2;
}
class Program
{
[STAThread]
static void Main()
{
var serializer = new XmlSerializer(typeof(TestSerialize));
using (XmlWriter writer = XmlWriter.Create(Guid.NewGuid() + ".xml"))
{
serializer.Serialize(writer, new TestSerialize() { Test1 = "hello", Test2 = 5 });
}
Console.ReadLine();
}
}