I have a class which looks like below.
[DataContract]
public class Applicant
{
private string titleField;
private string firstNamesField;
[DataMember(Order = 1)]
public string Title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
[DataMember(Order = 2)]
public string FirstNames
{
get
{
return this.firstNamesField;
}
set
{
this.firstNamesField = value;
}
}
}
I'm converting this class object into XML.
var app = new Applicant();
var dcs = new DataContractSerializer(app.GetType());
var ms = new MemoryStream();
dcs.WriteObject(ms, app);
string xml = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
var doc = XElement.Parse(xml);
the value of "doc" will look something like below.
<Applicant xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/testApp">
<Applicant>
<Title>Mr</Title>
<FirstNames>Applicant</FirstNames>
</Applicant>
</Applicant>
Now , how to add an attribute to a each element in xml. I want an XML like below.
<Applicant xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/testApp">
<Applicant>
<Title IsEnabled="True">Mr</Title>
<FirstNames IsEnabled="True">Applicant</FirstNames>
</Applicant>
</Applicant>
The "IsEnabled" attribute has to come for each element. But i don't want to parse XML and create attribute for each element. Is it possible to add a property in the class and make them as a attribute for each element ?