将值初始化为类对象时出错

将值初始化为类对象时出错

本文介绍了c# 序列化 xml(将值初始化为类对象时出错)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Silverlight

I am using Silverlight

我在初始化对象时出错:

I have an error on intialising the object which is:

Error   1   Duplicate initialization of member 'Parameter'  C:\Users\SHEK\Documents\Visual Studio 2012\Projects\SliderLastTry\SliderLastTry\ControlClass.cs 24  17  SliderLastTry

它对应于参数的第二个初始化:

and it corresponds to second intialistion of Parameter:

Parameter = { new Parameter { Name = "Name2", Label = "Label2", Unit = "Uint2", Component = { new Component { Type = "Type2", Attributes = { new Attributes { Type = "Slider", Displayed = "52", Selected = "05", Items = { "20", "21", "22", "23", "24" } } } } } } }

我正在尝试像这样在 Main 函数中分配初始化对象:

I am trying to assign the intialise the object in Main function like this:

                Parameter = { new Parameter { Name = "Name1", Label = "Label1", Unit = "Uint1", Component = { new Component { Type = "Type1", Attributes = { new Attributes { Type = "Combo", Displayed = "42", Selected = "02", Items = { "10", "11", "12", "13", "14" } } } } } } }
            ,
                Separator = { new Separator { Separators = "AutoSkew1" } }
                ,
                Parameter = { new Parameter { Name = "Name2", Label = "Label2", Unit = "Uint2", Component = { new Component { Type = "Type2", Attributes = { new Attributes { Type = "Slider", Displayed = "52", Selected = "05", Items = { "20", "21", "22", "23", "24" } } } } } } }

         var memory = new MemoryStream();
         var serial = new XmlSerializer(objectToSerialize.GetType());
         serial.Serialize(memory, objectToSerialize);
        var utf8 = new UTF8Encoding();
        return utf8.GetString(memory.GetBuffer(), 0, (int)memory.Length);

我知道原因为什么会出现此错误.但明知如此却还要这样做的原因是:

I know the reason why i have this error. But the reason for doing like this even after being aware of it is:

(1) 我必须按照我向您展示的顺序显示 xml.我的意思是参数,然后是分隔符,然后是参数.但是,如果我在第一次初始化时初始化我的对象,例如 Parameter = { new Parameter{..} , new Parameter{..}} 然后它在之后显示下一个但我想要 <separator>Auto Skew 在这两者之间.

(1) I have to display the xml in the same order as i have shown you. I mean Parameter then Separator an then again Parameter. But if i do intialize my object in the first intialization like Parameter = { new Parameter{..} , new Parameter{..}} then it show next just after the but i want <separator>Auto Skewing</separator> between these two .

如何解决这两个问题?如果您仍然不明白我在做什么,请不要犹豫,问我更多.非常感谢.

How to do solve these 2 problems ? Please do not hesitate to ask me more if you still dont understand what i am tryin to do. Thanks a lot.

我想它可以通过 XElement 完成,但是我仍然无法理解如何管理代码.

I guess it can be done through XElement but how to manage the code for that i am still not able to understand.

推荐答案

1: XmlSerializer 不支持该方案.要以这种格式构造 xml,您应该考虑使用 XElementXmlDocument 而不是 XmlSerializer.

1: XmlSerializer does not support that scenario. To construct xml in that format, you should consider using XElement or XmlDocument instead of XmlSerializer.

2:[XmlText],但是1"让这个没有意义

2: [XmlText], but "1" makes this moot

这篇关于c# 序列化 xml(将值初始化为类对象时出错)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:15