public
Created

A Readable way to create a simple C# Class and Serialize to XML

  • Download Gist
Object2Xml.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
namespace Object2Serializer
{
/// 2011-09-21 XmlSerialization
/// This class content (definition) was created due to a third-partys serialized stream of text (xml alike)
/// With data from i.e. a database, the lists Graph and Set should be populated. And then
/// .GenerateXmlText() will do the whole work! Easy extendable with GenerateJson and so on.
/// As a little side note. There are just three lines in the Serialize-method to make a xml-standard.
 
public static partial class Program
{
static void Main()
{
FlowChart c = new FlowChart();
 
c.Add(new FlowChart.Graph() { caption = "testcaption", xAxis = "xaxistest..", yAxis = "yaxistest.." });
 
c.Add(new FlowChart.Set() { color = "#001100", name = "Januari", value = "3" });
c.Add(new FlowChart.Set() { color = "#001100", name = "Februari", value = "183" });
c.Add(new FlowChart.Set() { color = "#002200", name = "Mars", value = "13" });
c.Add(new FlowChart.Set() { color = "#001144", name = "April", value = "32" });
c.Add(new FlowChart.Set() { color = "#731100", name = "Maj", value = "13" });
 
var text = c.GenerateXmlText();
var xml = c.GenerateXml();
 
Console.WriteLine(text);
Console.WriteLine();
Console.WriteLine(xml);
Console.ReadLine();
}
}
 
/// <summary>
/// Helper object for generate serialized flowchart string
/// </summary>
public class FlowChart
{
Graph graph;
 
public FlowChart()
{
this.graph = new Graph();
}
public string GenerateXmlText()
{
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
XmlWriterSettings writerSettings = new XmlWriterSettings() { OmitXmlDeclaration = true };
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Graph));
StringWriter textWriter = new StringWriter();
 
nameSpace.Add("", ""); // Fix to clear them out.
using (XmlWriter writer = XmlWriter.Create(textWriter, writerSettings))
xmlSerializer.Serialize(writer, graph, nameSpace);
 
return textWriter.ToString();
}
public string GenerateXml()
{
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
nameSpace.Add("", ""); // Fix to clear them out.
 
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Graph));
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, graph, nameSpace);
return textWriter.ToString();
}
 
public void Add(Graph graph)
{
this.graph = graph;
}
public void Add(Set set)
{
this.graph.set.Add(set);
}
 
/// <summary>
/// Graph Object Definition
/// </summary>
 
[XmlRoot("graph")]
public class Graph
{
[XmlArray("set")]
[XmlArrayItem("set", typeof(Set))]
public List<Set> set { get; set; }
 
[XmlAttribute("caption")]
public string caption { get; set; }
[XmlAttribute("xAxis")]
public string xAxis { get; set; }
[XmlAttribute("yAxis")]
public string yAxis { get; set; }
 
public Graph()
{
this.set = new List<Set>();
}
}
 
public class Set
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("value")]
public string value { get; set; }
[XmlAttribute("color")]
public string color { get; set; }
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.