Better approach to XML construction with some default values

I need to create a large XML document. The problem is that there are many defaults that will never change. Which is the best approach to use to generate the following XML snippet.

<Membership>
   <Clients>
      <Client>
        <FirstName>Bilbo</FirstName>
        <LastName>Baggins</LastName>
      </Client>
  </Client>
</Membership>

      

I need to build XML. What is the best approach considering the following:

Approach 1 (My current approach)

Instantiate the following classes, enter data from the form, and serialize it to XML.

public class Membership
{
    public List<Account> Accounts {get;set;}
    //Other Properties
} 

public class Account
{
    List<Client> Clients { get; set; }   
}

public class Client
{
   public ClientDetails Details { get; set; }
   public Boolean WebAccess     { get; set; }
}

public class ClientDetails
{
   public string FirstName      { get; set; }
   public string LastName       { get; set; }
}

      

The meaning of some properties depends on other properties, for example "Gender in the title" (if you define how mr then sets the gender as male). In my opinion, I cannot use auto-implementing properties like above to do this. I would not avoid this approach as it would involve a large constructor.

Approach 2

Load all XML documents using instances of various XML documents. For example, one file (Memberhip.xml) will define the general structure of the XML document, while another file will define the structure of the client only.

If I wanted to create a list of clients, I would create multiple instances of the client.xml file and add it to the Clients node for Membership.xml membership.

Is there a better approach?

+3


source to share


2 answers


If possible, use a database for this.



If you do not have the database option , I would recommend you to use multiple XML files (second option), since you can have multiple concurrent HTTP requests, processing will be more efficient with multiple files, better than 1 xml file common to all users.

+1


source


Maybe this will work for you:



    static void Main(string[] args)
    {
        List<KeyValuePair<string, string>> clientNames = new List<KeyValuePair<string, string>>();
        // substitute with values from a file or database
        clientNames.Add(new KeyValuePair<string, string>("Bilbo", "Baggins"));
        clientNames.Add(new KeyValuePair<string, string>("Frodo", "Baggins"));
        var xml = BuildXml(clientNames);
        Console.WriteLine(xml);
        Console.ReadKey();
    }

    static XElement BuildXml(IEnumerable<KeyValuePair<string, string>> clientNames)
    {
        XElement membership = new XElement("Membership");
        XElement clients = new XElement("Clients");
        membership.Add(clients);
        foreach (var kvp in clientNames)
        {
            XElement client = new XElement("Client");
            client.Add(new XElement("FirstName", kvp.Key), new XElement("LastName", kvp.Value));
            clients.Add(client);
        }
        return membership;
    }

      

+1


source







All Articles