I'm not really sure I understand what you're looking for exactly. You seem to have a data class that maps to the XML structure you have and that seems fine. I would remove any switch
logic in the code though as you're mixing data with the class definition and that probably isn't what you want to do.
public class MyClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Employee { get; set; }
public int Location { get; set; }
public string Description { get; set; }
}
It sounds like the problem you're having is that you have an XML file that links the same "user" to multiple Ids. It is unclear to me what your first XML file is supposed to represent. I assume it is part of a larger XML that you'll have so I would argue you need to split your data type into 2 pieces as well. The first piece is the user data (your second XML) and the second piece is whatever the first XML represents.
public class MyClass
{
public int Id { get; set; }
public User User { get; set; }
}
public classs User
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Employee { get; set; }
public int Location { get; set; }
public string Description { get; set; }
}
Can this all be in the same XML file? I'm sure it can but without having a better understanding of the relationship it would be hard to model properly.
<IDS>
<ID id="1">userId1</ID>
</IDS>
<USERS>
<USER id="userId1">
...
</USER>
</USERS>
It is up to the XML serializer to create the instances of your types based upon the XML. However object identification is beyond what it can do so you'll end up with multiple user objects for the same user. The only workaround that I'm aware of is to either write a custom ISerializable
for your ids
type that looks up a user in a list of already generated users. Alternatively just let the serializer do its thing and then after it has produced the objects in memory then go through and clean them up by using a dictionary to map user IDs to the 1 User
instance you want to use.