0

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); 
OpenStack
  • 5,048
  • 9
  • 34
  • 69
  • Use XmlSerializer and decorate the properties with the appropriate attributes like XmlElement / XmlAttribute / XmlArray / XmlRoot / etc. – Igor Jan 10 '20 at 19:20
  • Why are you using a JSON library to serialize something to XML? – Heretic Monkey Jan 10 '20 at 19:22
  • @HereticMonkey : I found it easy to work with. And it worked well when I was converting properties into elements. Does not work when I want to convert properties into attribute. – OpenStack Jan 10 '20 at 19:25
  • @Igor: i tried it. `Serialize` function does not have a return type. – OpenStack Jan 10 '20 at 19:26
  • Because it serializes the instance to a stream or writer of your choice. https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.serialize?view=netframework-4.8 – Igor Jan 10 '20 at 19:31
  • 3
    @OpenStack Igor's suggestion is how I would go about this too. Using Newtonsoft for this is, well, odd. Can you show us how you tried implementing Igor's suggestion? `XmlSerializer` gives you a tremendous amount of control over how your model is serialized. –  Jan 10 '20 at 19:31
  • @Amy Updated the question. – OpenStack Jan 10 '20 at 19:37
  • @OpenStack ... After reading my comment back to myself, I want to clarify: I do not mean to suggest that you *cannot* get this to work as expected using Newtonsoft. We can *probably* help with that. But because the usual process is to use `XmlSerializer`, that's what we know best, so we want to steer you in that direction. Personally, I do not remember ever using Newtonsoft to produce XML, so I would need to experiment to find a solution, if there is a solution. I hope this makes sense. That said, the code you added makes perfect sense, and we can *definitely* help with that. –  Jan 10 '20 at 19:38
  • Possible duplicate of [Serialize an object to string](https://stackoverflow.com/questions/2434534/serialize-an-object-to-string) – Igor Jan 10 '20 at 19:41
  • @Igor Thank you for quick help. – OpenStack Jan 10 '20 at 19:45
  • Does this answer your question? [Serialize an object to string](https://stackoverflow.com/questions/2434534/serialize-an-object-to-string) – Heretic Monkey Jan 10 '20 at 20:27

2 Answers2

3

Here is a complete solution. I recommend wrapping the writer in a using:

class Program
{
    static void Main(string[] args)
    {
        MyClass obj = new MyClass();
        obj.c = "1";
        obj.r = "2";
        obj.p = "3";
        obj.v = "4";

        var serializer = new XmlSerializer(obj.GetType());
        string result;
        using (var writer = new System.IO.StringWriter())
        {
            serializer.Serialize(writer, obj);
            result = writer.ToString();
        }

        Debug.WriteLine(result);
    }
}
0

as suggested by @Igor, this is what I did and it worked

       StringWriter writer = new StringWriter();
        var serializer = new XmlSerializer(obj.GetType());
        serializer.Serialize(writer, obj);
        var xml = writer.ToString();
OpenStack
  • 5,048
  • 9
  • 34
  • 69