I will be writing an XML file via C# where the data for the elements is dynamic. So typically, I'll have this structure (simplified for the question):
<?xml version="1.0" encoding="utf-8"?>
<Output xmlns="http://xxxx/xxx.xsd">
<AccountHolder>
<Name></Name>
<Address1></Address1>
<City></City>
<State></State>
<ZipCode></ZipCode>
</AccountHolder>
<Visits>
<VisitDate></VisitDate>
<Copay></Copay>
<CoInsurance></CoInsurance>
</Visits>
<PatientDetails>
<AcctNo></AcctNo>
<PatientName></PatientName>
<Medicare></Medicare>
<VisitDetails>
<VDate></VDate>
<Amount></Amount>
<NonCoveredAmount></NonCoveredAmount>
</VisitDetails>
</PatientDetails>
</Output>
Now, while there will always be one "Account Holder" there will be anywhere from 0 to multiple visits. Subsequently, there will be list of 0 or more Patients, and then nested within the patients, there will be 0 or more visit details.
I do not control the structure. I need to take the collected data I receive and create the XML. I'll be receiving data on a single account holder which may have any number of the subsequent elements.
I have classes for AccountHolder, Visit, PatientDetails, and VisitDetails. However, I am unsure as to the best method for building out the XML dynamically as I read the source data? At first, I was thinking on gathering the data in various collections.
Try following :
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Output output = new Output()
{
Accountholder = new AccountHolder()
{
Name = "",
Address1 = "",
City = "",
State = "",
ZipCode = ""
},
Visits = new Visits(),
PatientDetails = new PatientDetails()
{
AccNo = "",
PatientName = "",
Medicare = "",
VisitDetails = new List<VisitDetails>()
{
new VisitDetails()
}
}
};
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, "http://xxxx/xxx.xsd");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Output));
serializer.Serialize(writer, output);
}
}
[XmlRoot(Namespace = "http://xxxx/xxx.xsd")]
public class Output
{
public AccountHolder Accountholder { get; set; }
public Visits Visits { get; set; }
public PatientDetails PatientDetails { get; set; }
}
public class AccountHolder
{
public string Name { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
public class Visits
{
public DateTime VisitDate { get; set; }
public decimal Copay { get; set; }
public decimal CoInsurance { get; set; }
}
public class PatientDetails
{
public string AccNo { get; set; }
public string PatientName { get; set; }
public string Medicare { get; set; }
[XmlElement]
public List<VisitDetails> VisitDetails { get; set; }
}
public class VisitDetails
{
public DateTime VDate { get; set; }
public decimal Amount { get; set; }
public decimal NonCoveredAmount { get; set; }
}
}