开发者

Best practice create a C# Object which reflect and generate a Serialized string

This question may be asked or answered before but I feel that none of the hits really apply.

I would like to create a little class with attributes which will correspond to name and attributes in a output familiar to xml stream. The class should help the program to create a xml-alike string.

string test = "<graph caption='SomeHeader' attribute9='#someotherinfo'>" +
                 "<set name='2004' value='37800' color='AFD8F8' />" +
                 "<set name='2005' value='21900' color='F6BD0F' />" +
                 "<set name='2006' value='32900' color='8BBA00' />" +
                  "<set name='2007' value='39800' color='FF8E46' />" +
              "</graph>";

I think you got the idea. I have a static amount of known attributes which will be used in the tags. The only Tags here is set and graph.

I would like to do something like this,

Helper o = new Helper()
List<Tag> tag = new List<Tag>();
foreach (var someitem in somedatabaseresult)
{
   tag.Add(New Graph() { Caption = someitem.field , attribute9 = someitem.otherField });
   foreach (var detail in someitem)
   {
     tag.Add(new Set() { name = detail.Year, value = detail.Value color = detail.Color });
   }
}
o.Generate(); // Which will create the structure of result sample above

// and for future extension.. 
// o.GenerateXml();
// o.GenerateJson();

Please remember that this code is pesudo, just taken from my head. A result of that I ha开发者_如何学JAVAve some ideas but it take a day to code and test what best (or whorse).

What would be best practice to solve this task?

[EDIT]

This mysterious "Helper" is the (unluckily typed) class who contains a list of Graph, a list of Set and also (what I think about) contains all available attributes per Graph/Set object. The work of the foreach-loops above are mentioned to fill the Helper class with the data.

[EDIT2] Result here,

https://gist.github.com/1233331


Why not just create a couple of classes: Graph and Set. Graph would have a property of List<Set>.

In your foreach you can then create an instance or Graph and add instances of Set to its list.

When you're done use the XML Serializer to serialize the Graph object out to XML. Nice and easy to then output to another format as well if your needs change later e.g. serialize to JSON.

Edit following comment:

From top my head so may not be 100% correct...

var myGraph = BuildMeAGraph();
var serializer = new XmlSerializer(typeof(Graph));
var writer = XmlWriter.Create("myfile.xml");
serializer.Serialize(writer, myGraph);

But something like that should write it out to a file. If you want the XML in memory then write it out to an XMLTextWriter based on a memory stream instead and then you can write the contents to a string variable or do whatever you need with it.


If you want to create an XML, out of the object tree, then I think, you could try this:

XDocument doc = new XDocument
(
   somedatabaseresult.Select
   ( someitem =>
     new XElement("graph", 
                  new XAttribute("Caption", ""),
                  new XAttribute("attribute9", "#something"),
                  someitem.Select
                  (detail =>
                      new XElement("Set", 
                                   new XAttribute("name", "2003"),            
                                   new XAttribute("value", "34784"),
                                   new XAttribute("color", "#003300")
                  )    

   )

);

//save to file as XML
doc.Save("output.xml");

//save to local variable as XML string
string test = doc.ToString();

I wrote the save value for tags, as you've used in your code. However, I think you would like this:

new XAttribute("name", detail.name),            
new XAttribute("value", detail.value),
new XAttribute("color", detail.color)

Or whatever value you want to give to each attribute from the object detail.


Use the XmlSerializer.


I'd use the ExpandoObject, but I don't see the reason for what you are doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜