2

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.

Szandor
  • 23
  • 5
  • 1
    You really need that C_20 as a text node, on the same level as a child collection? – H H Mar 28 '12 at 15:45
  • Thanks for the tip, unfortunately I was not on my computer at work but at home I had no example to hand so that I wrote the XML code itself. Where do you write it, it fell on me, I just checked remotely and lo and behold ... Thank you, will not mend it ... although now I have what I need :) Thanks again – Szandor Mar 28 '12 at 21:12

2 Answers2

4

Based on your criteria, I would Implement IXmlSerializable on C_20. There is a great read here on SO on how to create the XML you are looking for.

Proper way to implement IXmlSerializable?

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Thank you very much for pointing that out. After I XMLSerialize to alternative settings and have been looking for that, I found no evidence that it could be so close beside it. The example is not that exactly what I was looking for a big thank you from me. – Szandor Mar 28 '12 at 19:50
2

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:

public string GetXml<T>(T instance)
{
    var type = typeof(T);
    var properties = type.GetProperties();
    var builder = new System.Text.StringBuilder();

    builder.AppendFormat("<{0}>", type.Name);

    foreach (var property in properties)
    {
        var name = property.Name;
        var value = property.InvokeMember(name, 
                                          BindingFlags.DeclaredOnly |         
                                          BindingFlags.Public |
                                          BindingFlags.NonPublic |
                                          BindingFlags.Instance |
                                          BindingFlags.GetProperty,
                                          null,
                                          instance,
                                          null);
        builder.AppendFormat("<{0}>{1}</{0}>", 
                             name,
                             value);          
    }

    builder.AppendFormat("</{0}>", type.Name);

    return builder.ToString();
}

That should at least point you in the right direction.

EDIT BY L.B

Same Idea, using Linq2Xml

public static string GetXml<T>(T obj)
{
    Type t = typeof(T);
    XElement xElem = new XElement("ENTITY");
    xElem.Add(t.Name,
              new XElement("ATTRIBUTES",
                           t.GetFields()
                            .Select(f => new XElement("ATTRIBUTE", 
                                                      new XAttribute("name",f.Name), 
                                                      f.GetValue(obj)))
                            .ToArray())
    );

    return xElem.ToString();
}
Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
  • I edited your answer. Feel free to delete it if you don't like. – L.B Mar 28 '12 at 17:58
  • Nope. Nice edit; just cleaned up a bit to make it more readable. I generally assume folks aren't that familiar with LINQ to XML so I provided the brute force method. This might be a daunting first time dive into LINQ to XML. :) – Mike Hofer Mar 28 '12 at 20:25
  • The first example I've taken not to run, but the second I wanted to test properly before anyway. I'm impressed that you managed to get it perfect with as little code as the result. Thank you for your absolute perfect example for me to come on and thanks to the first, I know even as I can make the import again. – Szandor Mar 28 '12 at 20:57
  • Okay, I think this version is better suited for me ... Since I have a lot of huge classes of them, I will not write all the code by hand where I come in then maybe small error ... – Szandor Mar 28 '12 at 21:51
  • @L.B, you should separate your edit into a separate answer so he can accept it and make it the right answer. You deserve the points, not me. :) – Mike Hofer Mar 29 '12 at 14:43
  • 1
    @MikeHofer Thanks. but custom serialization idea was from you. – L.B Mar 29 '12 at 14:53