0

It's silverlight project. I have the following XML :

<pars>
    <par>            
    </par>

    <s>Auto1</s>

    <par>           
    </par>      

    <par>
    </par>
</pars>

As you can see there are two Parameter (Under Parameters) and are one seperators .

All of them comes under Parameters in xml (And Parameters.cs in c# code).

What i am trying to do ? I have Following C# classes corresponding to that xml (Using the object of Parameters.cs class i am again trying to obtain the same xml).

Sss
  • 1,519
  • 8
  • 37
  • 67
  • take a look at this one http://stackoverflow.com/questions/3303165/using-xmlarrayitem-attribute-without-xmlarray-on-serializable-c-sharp-class – Jakub May 20 '14 at 12:35
  • @Jakub Thanks for the link but i dont have to use any inbuilt thing of Visual Studio. I have to make changesin my code in order to show the similar xml. Could you pleasehlp me in doing this ?? – Sss May 20 '14 at 12:41
  • This `[XmlElement("parameter")] public List Parameter { get; set; }` is not valid annotation for a list. You should use something like: `[XmlArray("parameterList")] [XmlArrayItem("parameter", typeof(Parameter))]` instead. – rosko May 20 '14 at 13:15
  • @rosko i have debugged it and this c# classed are working for deserialisation of xml that i have given at starting in code aand object is showing both xml name and values for each element on debugging for all and and it is working and i have to use – Sss May 20 '14 at 13:17

1 Answers1

0

First, i'm not sure that you need a construtor in the Parameters.cs and Parameter.cs, you could remove them.

Secondly, could you send us what is different between both xml ? Is it just the header ?

Try to change your serialization by this :

        using (MemoryStream ms = new MemoryStream())
        {
            //Serialization
            XmlSerializer xs = new XmlSerializer(typeof(ClassToSerialize));
            xs.Serialize(ms, ObjectToSerialize);
            ms.Position = 0;

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(ms);
        }
Alexein1
  • 109
  • 1
  • 6
  • I removed constructor and i have edited the snapshot of the output obtained on debugging. Its just that i have whown to you, I dont knbow why. – Sss May 20 '14 at 12:46
  • and its still same on removing constructor. – Sss May 20 '14 at 12:48
  • Yes the sentence for the construtor was just to say that is useless :) . The classes seems correct, could you try to change – Alexein1 May 20 '14 at 12:59
  • Change what ? in my class ? – Sss May 20 '14 at 13:01
  • How would complier know thta which Parameter element belongs to which in xml ? – Sss May 20 '14 at 13:03
  • Yes the sentence for the construtor was just to say that is useless :) . The classes seems correct, could you try to change your xmlroot `code`[XmlRoot(ElementName = "component")] like this by `code`[XmlRoot("component")] and try to add `code`ms.Position = 0; in class xml just after `code`serial.Serialize(..). `code`serial.Serialize(memory, objectToSerialize); memory.Position = 0; – Alexein1 May 20 '14 at 13:05
  • could you please write it in your answer its difficult to understand this code in comments ? – Sss May 20 '14 at 13:07
  • You can also remove [XmlRoot(ElementName = "parameter")] from the class "parameter" because you've already states it in the "parameters" class – Alexein1 May 20 '14 at 13:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54039/discussion-between-user234839-and-alexein1). – Sss May 20 '14 at 13:12
  • Actually you can remove all the Xmlroot which you've already states as an xmlelement in "parameters" class – Alexein1 May 20 '14 at 13:13
  • I commented all the XmlRoot in the class but still the same problem exists. – Sss May 20 '14 at 13:22
  • ok i see, try to change your serialization by this : using (MemoryStream ms = new MemoryStream()) { //Serialization XmlSerializer xs = new XmlSerializer(typeof(ClassToSerialize)); xs.Serialize(ms, ObjectToSerialize); ms.Position = 0; XmlDocument xDoc = new XmlDocument(); xDoc.Load(ms); } – Alexein1 May 20 '14 at 13:23
  • please write it in anser i am not able to understand here. – Sss May 20 '14 at 13:24
  • Do silverlight support XmlDocument ? – Sss May 20 '14 at 13:28
  • Error 1 The type or namespace name 'XmlDocument' could not be found (are you missing a using directive or an assembly reference? – Sss May 20 '14 at 13:29
  • iT IS NOT SUPPORTED by silverlight. and its equivalent in silverlight is XDocument from using System.Xml.Linq; and wheni do so it gives 1 error Error 1 Member 'System.Xml.Linq.XDocument.Load(System.IO.Stream)' cannot be accessed with an instance reference; qualify it with a type name instead CORREESPONDING TO " xDoc.Load(ms);" – Sss May 20 '14 at 13:33
  • and why you wanted me to change thecode like this ? – Sss May 20 '14 at 13:40
  • Because all is working fine when you deserialize? so I would have suspect the serialization – Alexein1 May 20 '14 at 14:21
  • In serialization, I have done a testing like i put "public string Name { get; set; }" in my Parameters.cs(Not parameter.cs) and i done " var pc = new Parameters() ; pc.Name = "shekharChk";" inside Main() in Xml.cs and it is showing ShekharChk in xml (with my code) – Sss May 20 '14 at 14:25
  • So I assume that you should have the same result with "Separator" element and that problem come from the "parameter" serialization. The xmlroot tag should be only marked in the "parameters" class because it's the root, keep it in the "parameters" class and remove all the others – Alexein1 May 20 '14 at 14:47
  • As a conclusion from what i understand from your comment is:(1) Just put [xmlRoot] only in Parameters.cs ( [XmlRoot(ElementName = "parameters")]) remove from other place :I HAVE DONE IT IS STILL SAME. – Sss May 20 '14 at 14:52
  • wHEN I put pouse on "pc" in line var xml = pc.ToXml(); (while debugging) it shows me Paramaeter (Null but with a plus sign),Separator (Null),Shekhar(Null). When i put mouse on Plus sign shown on Parameters, then it shows "static memebers" then it show "Non public Members" and then ""_emptyArray, {SliderLastTry.Parameter[0]} – Sss May 20 '14 at 14:56
  • Did you try in changing : – Alexein1 May 20 '14 at 21:39
  • Did you try to change : [XmlElement("parameter")] with[XmlArray("parameter")] indeed, parameter is a collection – Alexein1 May 20 '14 at 21:47
  • i HAVE done it.. how can it display the xml with out assigning items and values in the list :P – Sss May 21 '14 at 08:06
  • Have you foun a way with you issue ? :) What your xml looks like now ? – Alexein1 May 21 '14 at 08:20