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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for the answer but Why Question 1 makes you moot ?
@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.
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.
@user silverlight. 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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.