I can't see any obvious way of getting it to output those results without changing the classes radically... this might not be what you want, but by mirroring the desired output (not uncommon in DTOs) it gets the right result.
I can't see any obvious way of getting it to output those results without changing the classes radically... this might not be what you want, but by mirroring the desired output (not uncommon in DTOs) it gets the right result... Otherwise, you might be looking at IXmlSerializable, which is a huge pain: using System; using System.Collections. Generic; using System.Xml. Serialization; XmlRoot("response") public class MyResponse { public MyResponse() { Entries = new List(); } XmlElement("startIndex", Order = 1) public int StartIndex { get; set; } XmlElement("entry", Order = 2) public List Entries { get; set; } } public class Entry { public Entry() { } public Entry(Person person) { Person = person; } XmlElement("person") public Person Person { get; set; } public static implicit operator Entry(Person person) { return person == null?
Null : new Entry(person); } public static implicit operator Person(Entry entry) { return entry == null? Null : entry. Person; } } public class Person { XmlElement("name") public string Name { get; set; } } static class Program { static void Main() { MyResponse resp = new MyResponse(); resp.
StartIndex = 1; resp.Entries. Add(new Person { Name = "meeee" }); resp.Entries. Add(new Person { Name = "youuu" }); XmlSerializer ser = new XmlSerializer(resp.GetType()); ser.
Serialize(Console. Out, resp); } }.
IXmlSerializable should be better than what I have currently, ie. Serializing and deserializing via reflection only – seanlinmt Jun 12 '09 at 8:31 I don't really want to change the structure of the classes too much. I still have DataContract attributes for serializing to JSON.
And the example given is a simplified version of the classes. I can decorate using certain XMLxxx and Dataxxx attributes at the same time right? This way I can have different formats when serializing to JSON or XML.
– seanlinmt Jun 12 '09 at 8:45 Yes, but I'm not sure how that helps if XmlSerializer doesn't want to write things the way you want... – Marc Gravell? Jun 12 '09 at 9:28 It seems that with IXmlSerializable, I will have to explicitly specify each and every field that I want to serialize :( – seanlinmt Jun 12 '09 at 9:31 The reason of the explicit format is due to opensocial. Org/Technical-Resources/opensocial-spec-v09/… – seanlinmt Jun 12 '09 at 9:32.
Sorry for the misleading title. It's fixed now. As for excluding default values.
I use public DateTime? Birthday { get; set; } XmlIgnore public bool birthdaySpecified { get { return birthday. HasValue; } } – seanlinmt Jun 12 '09 at 8:33 The xxxSpecified does the job ok for me so far... but the main problem is the nesting of sub objects.
– seanlinmt Jun 12 '09 at 8:33 I have to be honest, I am unsure of where the code in my answer is failing you.. You can't get a clean solution out of the box just using XML Attributes, and as Mark correctly says, in cases like this implementing IXmlSerializable is a pain. Basically you need to roll your own little bit of serialization to handle the "generic" bit.. Have you reviewed the code in my answer? Is there anything I can help clarify?
– Rob Cooper Jun 12 '09 at 11:10 Sorry Marc, just realised I incorrectly spelled your name. My apologies. – Rob Cooper Jun 12 '09 at 11:13 I wish I can get a clean solution out of the box.
I mean my question seems quite straight forward right? So I assume to the solution should be quite straight forward instead of beating around the bush? – seanlinmt Jun 12 '09 at 16:15.
I've found a way to do this without using IXmlSerializable. Use the XmlDictionaryWriter for formatting necessary parts and for the rest just stick with the DataContractSerializer. I created an interface for MyCollection public interface IMyCollection { int getStartIndex(); IList getEntry(); } Therefore, MyCollection is no longer decorated with any XMLxxxx attributes.
And the method to convert to string is as follows. Can it be improved? Public string ConvertToXML(object obj) { MemoryStream ms = new MemoryStream(); using (XmlDictionaryWriter writer = XmlDictionaryWriter.
CreateTextWriter(ms, Encoding. UTF8, true)) { writer. WriteStartDocument(); if (obj is IMyCollection) { IMyCollection collection = (IMyCollection)obj; writer.
WriteStartElement("response"); writer. WriteElementString("startIndex","0"); var responses = collection.getEntry(); foreach (var item in responses) { writer. WriteStartElement("entry"); DataContractSerializer ser = new DataContractSerializer(item.GetType()); ser.
WriteObject(writer, item); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.Flush(); return Encoding. UTF8. GetString(ms.ToArray()); }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.