I didn't really know how to title this problem as it's pretty context sensitive...
I am deserializing xml into the following class:
[XmlType("user")]
public class User
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public byte Id { get; set; }
[XmlAttribute("email")]
public string Email { get; set; }
[XmlAttribute("cellphone")]
public string CellPhone { get; set; }
}Obviously if the user is in the xml file then he exists and has a id. It should be set to the value that is indicated in the xml file. However, when I programatically add a user (from the UI for example) then I want to create a GUID using System.Guid. The assignment of the user Id should not be up to the programmer but should be automatic from within the User class.
If it wasn't for the xml deserialization I'd simply put a call to NewGuid in the constructor of User and then assign it to Id.
Is there anything I can do to get around this?
-- EDIT --
Perhaps
[XmlType("user")]
public class User
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public Guid Id
{
get
{
return this.Id;
}
set
{
if (value != null)
this.Id = value;
else
this.Id = Guid.NewGuid();
}
}
[XmlAttribute("email")]
public string Email { get; set; }
[XmlAttribute("cellphone")]
public string CellPhone { get; set; }
}This is only automatic when the deserializer loads data. I need to figure out how to trigger the property set when I add a user programatically.

1 answers
I think that something like this should work
[XmlType("user")]
public class User
{
[XmlAttribute("name")]
public string Name { get; set; }
private byte _id;
[XmlAttribute("id")]
public byte Id
{
get { return _id; }
set
{
_id = value;
this.Guid = System.Guid.NewGuid();
}
}
[XmlAttribute("email")]
public string Email { get; set; }
[XmlAttribute("cellphone")]
public string CellPhone { get; set; }
[XmlIgnore]
public Guid Guid { get; private set; }
}
EDIT - see comments below
It may be that I've misunderstood the question.
However, there's no escaping that, if 'id' is a property of the 'user' element in the XML file, then you'll have to have a public Id property of the same type (i.e. byte) in your C# User class for deserialization purposes and you'll then have to generate your Guid programatically though you can make this read-only.
If you want to prevent someone resetting the Id property (and hence the Guid) programatically, then you could try my edited code below.
Notice that as Id is a byte (i.e. a value type), you can't use 'null' as in your own edit - you need to choose some other 'impossible' value as the default:
To allow for the programatic creation of new User objects, I'd introduce a constructor which takes the appropriate arguments. The deserializer should ignore this.
answered 5 months ago by:
11603
499
Sorry... not sure I follow. The way I see it _id will be set to value no matter where the assignment is coming from (deserialization or post-deserialization).
499
I edited my original post....
11603
Please see my own edit above.
499
Your last statement about the constructor is giving me issues. Apparently if the constructor is not parameter-less the xml deserializer will not work. I get the following exception: {"ConsoleApplication1.User cannot be serialized because it does not have a parameterless constructor."}.
11603
Just add an empty parameterless constructor to the User class as well as the one which takes parameters and it should work. When you had no constructors in the User class, the system provided an empty parameterless one but now you're adding a constructor which takes parameters you also need to add the parameterless one manually for deserialization purposes.
499
Ahhh! So I am basically overloading the constructor. Makes sense!