I’m looking to convert for a way to my classes to XML and back again.
This works even with the Serialize, but unfortunately I need a completely different issue.
Unfortunately, I have indeed sometimes found a few things around the serializer convert something, but unfortunately, it almost seems like there’s no way on this goal.
So I thought it must be possible to read all the variables but simply without manually typing hardcoded. Maybe someone here has a tip for me.
This is a short test of one of the Classes:
public class C_20 //GENERAL DATA
{
public string OBJAP = "test";
public string AKTYP = "1";
public string RLTP1 = "2";
public string ROLE1 = "3";
}
And I need this formated XML:
<ENTITY name="C_20">
<ATTRIBUTES>
<ATTRIBUTE name="OBJAP">test</ATTRIBUTE>
<ATTRIBUTE name="AKTYP">1</ATTRIBUTE>
<ATTRIBUTE name="RLTP1">2</ATTRIBUTE>
<ATTRIBUTE name="ROLE1">3</ATTRIBUTE>
</ATTRIBUTES>
</ENTITY>
I hope I can help someone who otherwise have to write several thousand lines by hand, which would obviously be very error prone.
It sounds to me like you want to pass an object instance to a serializer, and have that serializer automatically enumerate all the properties on the instance, and return them as an XML string.
Good news: there are several ways to do this. You might want to check out the System.Reflection namespace, which contains all sorts of types to help you inspect an object at runtime.
As a simple, brute force example:
That should at least point you in the right direction.
EDIT BY L.B
Same Idea, using Linq2Xml