Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Readable way to create a simple C# Class and Serialize to XML
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; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.