aherlambang
aherlambang

Reputation: 14418

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-27e971b903ed.xml

I am just curious on how to do such thing?

Upvotes: 1

Views: 219

Answers (4)

Matt Dearing
Matt Dearing

Reputation: 9386

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

Upvotes: 3

Brian
Brian

Reputation: 4984

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

Upvotes: 2

Development 4.0
Development 4.0

Reputation: 2753

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.

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126932

Look at System.Guid.

Guid guid = System.Guid.NewGuid();

Upvotes: 2

Related Questions