C# class with Many to One XML data

Csick 21 Reputation points
2022-04-08T12:39:07.48+00:00

Hi all,

Having trouble designing a Many to One xml class ... Currently accomplishing the requirements in a class with a switch statement,

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

    public MyClass(int id)
    {
        switch (id)
        {
            case 1:
            case 2:
            case 100:
            case 200:
                this.FirstName = "Joe";
                this.LastName = "Dee";
                this.Employee = true;
                this.Location = 11;
                this.Description = "Bla Bla Bla";
                break;

            case 3:
            case 4:
            case 50:
            case 60:
            case 600:
            case 700:
            case 800:
                this.FirstName = "Jane";
                this.LastName = "Doe";
                this.Employee = false;
                this.Location = 11;
                this.Description = "Bla Bla Bla";
                break;
        }
    }
}

I tried using two xml files linked by a unique id

<IDS>
  <ID id="1">UniqueID1</ID>
  <ID id="2">UniqueID1</ID>
  <ID id="100">UniqueID1</ID>
  <ID id="200">UniqueID1</ID>
  <ID id="3">UniqueID2</ID>
  <ID id="4">UniqueID2</ID>
  <ID id="50">UniqueID2</ID>
  <ID id="60">UniqueID2</ID>
  <ID id="600">UniqueID2</ID>
  <ID id="700">UniqueID2</ID>
  <ID id="800">UniqueID2</ID>
</IDS>

and

<USERS>
  <USER id="UniqueID1">
    <FIRSTNAME>Joe</FIRSTNAME>
    <LASTNAME>Dee</LASTNAME>
    <EMPLOYEE>True</EMPLOYEE>
    <LOCATION>11</LOCATION>
    <DESCRIPTION>Bla Bla Bla</DESCRIPTION>
  </USER>
  <USER id="UniqueID2">
    <FIRSTNAME>Jane</FIRSTNAME>
    <LASTNAME>Doe</LASTNAME>
    <EMPLOYEE>False</EMPLOYEE>
    <LOCATION>11</LOCATION>
    <DESCRIPTION>Bla Bla Bla</DESCRIPTION>
  </USER>
</USERS>

Don't really like the two file approach in the first place, I can live with it.
However I was unable to pull it all together into a working package.

Any guidance would be much appreciated.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 51,346 Reputation points
    2022-04-08T15:07:46.87+00:00

    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.


0 additional answers

Sort by: Most helpful