Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have a class which looks like below.

C#
[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.

C#
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.

XML
<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.

XML
<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 ?
Posted
Updated 4-May-14 20:53pm
v2
Comments
ZurdoDev 2-May-14 10:09am    
If you have the xsd there are tools to covnert xsd to C# classes.
Maciej Los 2-May-14 12:28pm    
Could you share a link to that tool?
ZurdoDev 2-May-14 12:33pm    
Actually, I believe Visual Studio has one built in.

I believe this is the one I have used, https://xsd2code.codeplex.com/
Sergey Alexandrovich Kryukov 2-May-14 12:55pm    
You better explain why doing so. All you do looks quite weird: first, you use Data Contract serializer, but then, you use use XElement on output. I hardly can imagine how can it make sense. Please, what's the purpose of it?
—SA
Abhishek Sivasubramanian 2-May-14 16:26pm    
What I would suggest is to use the DataContractSerializer to generate serialized form of the DataContract class. Now prepare an XmlDictionaryWriter object. You could use the WriteAttributeString() method to add attributes to these XML elements. As Sergey said, you need not create XElemenet and parse stuff when DataContract serializer does all this for you.

Applicant p = new Applicant();
DataContractSerializer dcs = new DataContractSerializer(typeof(Applicant));
XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(someStream,Encoding.UTF8 );
dcs.WriteObject(xdw, p);

dcs.WriteStartObject(xdw, p);
xdw.WriteAttributeString("IsEnabled", "True");
dcs.WriteObjectContent(xdw, p);
dcs.WriteEndObject(xdw);

Does this help ?

1 solution

Below article explains similar problem and provide a solution. Please check

DataContract XML serialization and XML attributes[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900