These classes would serialize to the XML you want.
public class Job
{
[XmlElement(Type = typeof(Company))]
public Company Company { get; set; }
public Location Location { get; set; }
}
public class Company
{
[XmlTextAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Active { get; set; }
}
public class Location
{
public string City { get; set; }
[XmlAttribute]
public int Local { get; set; }
}
so..
List<Job> jobs = new List<Job>();
jobs.Add(new Job() { Company = new Company { Name = "ACME", Active = "Yes" }, Location = new Location() { City = "Hollywood", Local = 1} });
jobs.Add(new Job() { Company = new Company { Name = "Black Mesa", Active = "No" }, Location = new Location() { City = "New Mexico", Local = 3 } });
XmlSerializer x = new XmlSerializer(jobs.GetType(), new XmlRootAttribute("Jobs"));
x.Serialize(Console.Out, jobs);
Console.ReadLine();
will produce..
<?xml version="1.0" encoding="ibm850"?>
<Jobs xmlns:xsi="http://ift.tt/Atvu06; xmlns:xsd="http://ift.tt/FPH52k;
<Job>
<Company Active="Yes">ACME</Company>
<Location Local="1">
<City>Hollywood</City>
</Location>
</Job>
<Job>
<Company Active="No">Black Mesa</Company>
<Location Local="3">
<City>New Mexico</City>
</Location>
</Job>
</Jobs>
No comments:
Post a Comment