I don't know what the "best" way is, but I'd think using a GUID would be a sure-fire way of getting a unique value for your ID field.
If you wanted an alternative method that uses a smaller number, you could try checking the file each time prior to inserting, and getting the next available ID that's one larger than the previous:
private int GenerateNextId()
{
var file = XDocument.Load("yourFile.xml"); // or pass an XDocument in
// so you don't have to reload it
return file.Descendants("SomeElement")
.OrderByDescending(x => Convert.ToInt32(x.Attribute("ElementId").Value))
.Select(x => Convert.ToInt32(x.Attribute("ElementId").Value))
.FirstOrDefault() + 1;
}
This is just posted as an alternative. I don't know how efficient this is as your XML grows in size. YMMV
If you decide to keep using the GUID, there are ways to shorten it, such as this SO post:
Convert.ToBase64String(Guid.NewGuid().ToByteArray());
I tried it out - the generated ID is nearly cut in half:
0b427c5a-1541-4cb4-8995-4e67dac61654
WnxCC0EVtEyJlU5n2sYWVA==
d1205a49-f64b-4418-8449-b1cd52f06624
SVog0Uv2GESESbHNUvBmJA==