问题描述
我程序里要用到几种自定义的对象,一共是9个对象。每一个对象有不同的属性(我把它定义成结构体了)。其中对象1的属性是结构体LEDWindowsAttribute对象2、对象3的属性是结构体TextWindowsAttribute对象4、对象5、对象6、对象7、对象8、对象9的属性是结构体TimeWindowsAttribute对象1是父节点(只有一个)对象2------对象8是子节点。我通过程序上的按钮操作动态的增加或删除子节点(用TreeView显示父节点和子节点,子节点里有重复对象比如有3个对象2,且子节点数量可能很多)。我怎么才能将节点关系及对象属性序列化并存储成XML格式的文件,怎么反序列化XML文件解析还原得到TreeView上的树状结构和各个节点的属性。希望大神能给出代码或思路。TreeView上的表现形式如下所示对象1的NodeText值|----对象2的NodeText值|----对象3的NodeText值|----对象4的NodeText值|----对象5的NodeText值|----对象2的NodeText值|----对象6的NodeText值|----对象3的NodeText值。。。。。。以下是我定义的几个对象属性//////单行文字和多行文字对象窗口的属性///publicstructTextWindowsAttribute{[XmlAttribute("NodeName")]publicstringNodeName;//节点名字[XmlElement("NodeText")]publicstringNodeText;//节点文本[XmlElement("NodeIndex")]publicintNodeIndex;//节点序号[XmlElement("X")]publicintX;//X坐标[XmlElement("Y")]publicintY;//Y坐标[XmlElement("Width")]publicintWidth;//宽度[XmlElement("Height")]publicintHeight;//高度[XmlElement("Text")]publicstringText;//文字[XmlElement("FontName")]publicstringFontName;//字体[XmlElement("FontSize")]publicfloatFontSize;//字号[XmlElement("FontBold")]publicboolFontBold;//是否粗体[XmlElement("FontItalic")]publicboolFontItalic;//是否斜体[XmlElement("FontUnderline")]publicboolFontUnderline;//是否有下划线[XmlElement("FontColor")]publicintFontColor;//文字颜色}//////时间对象窗口的属性///publicstructTimeWindowsAttribute{[XmlAttribute("NodeName")]publicstringNodeName;//节点名字[XmlElement("NodeText")]publicstringNodeText;//节点文本[XmlElement("NodeIndex")]publicintNodeIndex;//节点序号[XmlElement("X")]publicintX;//X坐标[XmlElement("Y")]publicintY;//Y坐标[XmlElement("Width")]publicintWidth;//宽度[XmlElement("Height")]publicintHeight;//高度[XmlElement("FontName")]publicstringFontName;//字体[XmlElement("FontSize")]publicfloatFontSize;//字号[XmlElement("FontBold")]publicboolFontBold;//是否粗体[XmlElement("FontItalic")]publicboolFontItalic;//是否斜体[XmlElement("FontUnderline")]publicboolFontUnderline;//是否有下划线[XmlElement("FontColor")]publicintFontColor;//文字颜色}//////LED显示屏对象窗口的属性///publicstructLEDWindowsAttribute{[XmlAttribute("NodeName")]publicstringNodeName;//节点名字[XmlElement("NodeText")]publicstringNodeText;//节点文本[XmlElement("NodeIndex")]publicintNodeIndex;//节点序号[XmlElement("X")]publicintX;//X坐标[XmlElement("Y")]publicintY;//Y坐标[XmlElement("Width")]publicintWidth;//宽度[XmlElement("Height")]publicintHeight;//高度}
解决方案
解决方案二:
解决方案三:
[Flags]publicenumNodeFontStyle{Regular=0,Bold=1,Italic=2,Underline=4,All=Bold|Italic|Underline,}[Serializable]publicstructNodeRect{[XmlAttribute("x")]publicintX{get;set;}[XmlAttribute("y")]publicintY{get;set;}[XmlAttribute("width")]publicintWidth{get;set;}[XmlAttribute("height")]publicintHeight{get;set;}publicNodeRect(intx,inty,intwidth,intheight){X=x;Y=y;Width=width;Height=height;}}[Serializable]publicclassNodeFont{[XmlAttribute("name")]publicstringName{get;set;}[XmlAttribute("size")]publicfloatSize{get;set;}[XmlElement("color")]publicSystem.Drawing.ColorColor{get;set;}[XmlAttribute("style")]publicSystem.Drawing.FontStyleStyle{get;set;}staticNodeFont_Default;publicstaticNodeFontDefault{get{return_Default;}}staticNodeFont(){_Default=newNodeFont(){Name=System.Drawing.SystemFonts.DefaultFont.Name,Size=System.Drawing.SystemFonts.DefaultFont.Size,Color=System.Drawing.SystemColors.ControlText,Style=System.Drawing.FontStyle.Regular,};}}[Serializable][XmlInclude(typeof(LedNode))][XmlInclude(typeof(TimeNode))][XmlInclude(typeof(TextNode))]publicabstractclassNodeBase{[XmlAttribute("name")]publicstringName{get;set;}[XmlAttribute("text")]publicstringText{get;set;}[XmlAttribute("index")]publicintIndex{get;set;}[XmlElement("rect")]publicNodeRectRect{get;set;}NodeBase_Parent;publicNodeBaseParent{get{return_Parent;}}List<NodeBase>_Children;[XmlArray("nodes")][XmlArrayItem("node")]publicList<NodeBase>Children{get{return_Children;}}publicNodeBase(){_Children=newList<NodeBase>();_Parent=null;}publicvoidSetParent<T>(Tparent)whereT:NodeBase{_Parent=parent;}}[Serializable][XmlType("led")]publicclassLedNode:NodeBase{publicstaticreadonlyNodeRectDefaultRect;staticLedNode(){DefaultRect=newNodeRect(0,0,300,90);}publicLedNode(){Rect=DefaultRect;}}[Serializable][XmlType("time")]publicclassTimeNode:LedNode{publicnewstaticreadonlyNodeRectDefaultRect;[XmlElement("font")]publicNodeFontFont{get;set;}staticTimeNode(){DefaultRect=newNodeRect(0,0,100,80);}publicTimeNode(){Font=NodeFont.Default;}}[Serializable][XmlType("text")]publicclassTextNode:TimeNode{publicstaticnewreadonlyNodeRectDefaultRect;[XmlAttribute("caption")]publicstringCaption{get;set;}staticTextNode(){DefaultRect=newNodeRect(0,0,200,80);}publicTextNode(){Font=NodeFont.Default;}}publicstaticclassNodeStaticExtension{publicstaticstringSerialize<T>(thisTnode)whereT:NodeBase{varsettings=newXmlWriterSettings{Encoding=Encoding.Default,Indent=true,OmitXmlDeclaration=true,};varns=newXmlSerializerNamespaces();ns.Add(string.Empty,string.Empty);varsb=newStringBuilder();using(varwriter=XmlWriter.Create(sb,settings)){varserializer=newXmlSerializerFactory().CreateSerializer(typeof(T));serializer.Serialize(writer,node,ns);}returnsb.ToString();}}publicstaticclassNodeFactory{publicstaticTCreate<T>(stringname,stringtext,intindex)whereT:NodeBase{varresult=Activator.CreateInstance<T>();result.Name=name;result.Text=text;result.Index=index;returnresult;}publicstaticTCreate<T>(stringname,stringtext,intindex,stringcaption)whereT:TextNode{varresult=Create<T>(name,text,index);result.Caption=caption;returnresult;}publicstaticTCreate<T>(stringname,stringtext,intindex,NodeBaseparent)whereT:NodeBase{varresult=Create<T>(name,text,index);result.SetParent(parent);returnresult;}publicstaticTCreate<T>(stringname,stringtext,intindex,stringcaption,NodeBaseparent)whereT:TextNode{varresult=Create<T>(name,text,index);result.Caption=caption;returnresult;}publicstaticTCreate<T>(stringname,stringtext,intindex,NodeBaseparent,IEnumerable<T>children)whereT:NodeBase{varresult=Create<T>(name,text,index);result.SetParent(parent);result.Children.AddRange(children);returnresult;}publicstaticTCreate<T>(stringname,stringtext,intindex,stringcaption,NodeBaseparent,IEnumerable<T>children)whereT:TextNode{varresult=Create<T>(name,text,index);result.Caption=caption;result.SetParent(parent);result.Children.AddRange(children);returnresult;}publicstaticTCreate<T>(stringxml)whereT:NodeBase{varresult=default(T);varserializer=newXmlSerializerFactory().CreateSerializer(typeof(T));using(varsr=newSystem.IO.StringReader(xml)){using(varxr=XmlReader.Create(sr)){if(serializer.CanDeserialize(xr)){result=serializer.Deserialize(xr)asT;}}}returnresult;}}
解决方案四:
调用方法,box是一个textbox至于如何加载到treeview就靠你自己了varroot=NodeFactory.Create<LedNode>("LED","LED",0);for(inti=0;i<8;i++){vars=string.Format("Text{0}",i+1);varchild=NodeFactory.Create<TextNode>(s,s,i,s,root);if(i%3==0){child.Font.Color=System.Drawing.Color.Red;child.Font.Style=System.Drawing.FontStyle.Bold|System.Drawing.FontStyle.Underline;}root.Children.Add(NodeFactory.Create<TextNode>(s,s,i,s,root));}for(inti=0;i<3;i++){vars=string.Format("Time{0}",i+1);varchild=NodeFactory.Create<TimeNode>(s,s,i,root);if(i==2){child.Font.Color=System.Drawing.Color.Blue;child.Font.Style=System.Drawing.FontStyle.Bold|System.Drawing.FontStyle.Italic;}root.Children.Add(child);}varxml=root.Serialize();box.AppendText(xml);varnode=NodeFactory.Create<LedNode>(xml);foreach(varchildinnode.Children){box.AppendText(string.Format("{0}:{1}rn",child.GetType(),child.Name));}