1

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" } } } } } } }

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" } } } } } } }

and

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

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

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Sss
  • 1,519
  • 8
  • 37
  • 67

1 Answers1

1

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], but "1" makes this moot

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for the answer but Why Question 1 makes you moot ? – Sss May 21 '14 at 13:19
  • @user234839 because getting `XmlSerializer` to do *one* thing, when it doesn't support the other *at all*, isn't helpful. If you switch to `XmlDocument` or `XDocument`/`XElement`, 2 no longer applies. – Marc Gravell May 21 '14 at 13:21
  • And is it supported by silverlight5 ? and does it support the xml formatting ? because i have to display idented(well structured, Not ina continous single line) display .It would be my next step after this. – Sss May 21 '14 at 13:26
  • @user [silverlight](http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.95).aspx). IIRC the default behaviour of `Save(...)` is to output indented, with options to do other things - but it should take only minutes for you to investigate this. – Marc Gravell May 21 '14 at 13:28
  • Thanks (I asked you in adavance because like xmlElement if i do all and at last i know that it dont format then it waste lot of work and time as well to restart with new way). Could you please give me a very small sample Because i am not able to underdqtnd how to manage the different classes that i have to create xml. Because i had [XmlRoot(ElementName = "parameters")] for root class [XmlElement("parameter")] for elements and [XmlArray("items")] [XmlArrayItem("item", typeof(string))] for arrays. – Sss May 21 '14 at 13:40
  • @user234839 did you look at the examples on MSDN? Basically, something like http://pastie.org/9196051 – Marc Gravell May 21 '14 at 13:44
  • Ok let me try..Will come soon to mark you as answer as it works. Thanks – Sss May 21 '14 at 13:47
  • I have one error when i do that (could you please see in chat) – Sss May 21 '14 at 13:58
  • @user234839 no, I'm not conveniently available for realtime chat right now – Marc Gravell May 21 '14 at 13:58
  • Marc the problem in your code is it even do not enter once in foreach loop if i make it parameters.Parameter and if i keep your code then Parameters is not a list so it gives error (where Parameters parameters =new Parameters();) – Sss May 21 '14 at 15:24
  • Could you lease help me on this link i am still not able to do that. http://stackoverflow.com/questions/24383991/how-to-use-xelement-in-this-situation – Sss Jun 24 '14 at 11:36
  • Could you lease help me on this link i am still not able to do that. http://stackoverflow.com/questions/24383991/how-to-use-xelement-in-this-situation – Sss Jun 24 '14 at 12:50