开发者

XML serializer filename

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-27e971b开发者_StackOverflow中文版903ed.xml

I am just curious on how to do such thing?


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();
    }
}


Look at System.Guid.

Guid guid = System.Guid.NewGuid();


var fileName = String.Format("{0}.xml", System.Guid.NewGuid().ToString());


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜