I am trying to convert properties of a C# class into XML attribute.
public class MyClass
{
[XmlAttribute]
public string c { get; set; }
[XmlAttribute]
public string r { get; set; }
[XmlAttribute]
public string p { get; set; }
[XmlAttribute]
public string v { get; set; }
}
MyClass obj = new MyClass();
obj.c = "1";
obj.r = "2";
obj.p = "3";
obj.v = "4";
Now converting class into XML using newton soft:
dynamic obj2 = new ExpandoObject();
obj2.data = obj;
var json = JsonConvert.SerializeObject(obj2);
XNode node = JsonConvert.DeserializeXNode(json, "root");
produces something like this:
<root>
<data>
<c>1</c>
<r>2</r>
<p>3</p>
<v>4</v>
</data>
</root>
But I want something like this:
<root>
<data c="1" r="2" p="3" v="4" />
</root>
I want to convert properties into attributes, not elements.
When I use XML Serializer, I want a string out and don't want to write data to a file or stream.
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(Console.Out, obj);