最佳实践创建一个反映和生成序列化字符串的C#对象 [英] Best practice create a C# Object which reflect and generate a Serialized string

查看:137
本文介绍了最佳实践创建一个反映和生成序列化字符串的C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能会在之前提出或回答,但我觉得没有一个命中真的适用。

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

我想创建一个带有属性的小类到XML流中熟悉的输出中的名称和属性。类应该帮助程序创建一个类似xml的字符串。

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.

我想这样做,

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();

请记住,这段代码是pesudo,只是从我的头。结果我有一些想法,但它需要一天的代码和测试什么是最好的(或whorse)。

Please remember that this code is pesudo, just taken from my head. A result of that I have some ideas but it take a day to code and test what best (or whorse).

最好的做法是解决这个任务?

What would be best practice to solve this task?



这个神秘的帮助器是(不幸的类型)类,包含一个列表的列表,一个Set列表,并且(我想)包含每个Graph / Set对象的所有可用属性。上面提到的foreach循环的工作是填充Helper类中的数据。


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.


在这里结果,

https://gist.github.com/1233331

推荐答案

为什么不创建几个类:图和集。图表将具有 List< Set> 的属性。

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

code>,然后可以创建一个实例或图表,并将Set的实例添加到它的列表中。

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

完成后使用XML Serializer对象输出到XML。尼斯和容易,然后输出到另一种格式,如果你的需求改变,例如。序列化到JSON。

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.

编辑以下注释:

从头开始,所以可能不是100%正确。 ..

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);

但是类似的东西应该写入一个文件。如果你想在内存中的XML,然后写出一个基于内存流的XMLTextWriter,然后你可以写内容到一个字符串变量或做任何你需要的东西。

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.

这篇关于最佳实践创建一个反映和生成序列化字符串的C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆