hi, classes that are collections only get the collection itself serialized, any other public properties on the class are ignored. it is documented at [1], where you can read the following:
Items That Can Be Serialized Public read/write properties and fields of public classes. Classes that implement ICollection or IEnumerable. (Note that only collections are serialized, not public properties.) XmlElement objects. XmlNode objects. DataSet objects. hth, franz [1] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconintroducingxmlserialization.asp -----Ursprüngliche Nachricht----- Von: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] Im Auftrag von J. Merrill Gesendet: Donnerstag, 31. August 2006 00:08 An: [email protected] Betreff: Re: [ADVANCED-DOTNET] Need some Xml Serialization/Deserialziation assitance... What happens if you set m.Orders.TestAttribute to something? (How should a null property value be serialized to XML?) At 10:57 AM 8/30/2006, Mike Andrews wrote >I really need some help with this. >The problem is that a collection class is not serializing it's "properties" >that I've defined (and marked as an XmlAttribute) it will not export >the attribute properly. >I've listed below some sample code the demonstrates this. >As a result, here's what you get: > ><?xml version="1.0" encoding="utf-16"?> <Master >xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" >http://www.w3.org/2001/XMLSchema"> > <orders> > <order id="1a055971-8324-44dd-aacf-f6e9fe982737" /> > <order id="50c4b5b1-d2ba-419b-9c1c-e80169ad738e" /> </orders> ></Master> > >Notice that on the <orders> element there is no attribute. >If you look at the sample code below within the Orders class, I created >a public field called TestAttribute and put an XmlAttribute >serialization declaration around it. > >Any help would be greatly appreciated. > >Thanks, >Mike > >Here's the sample code: > > public static void SetOrders() { > > Master m = new Master(); > Order o = null; > > o = new Order(); > o.id = Guid.NewGuid(); > > m.Orders.Add(o); > > o = new Order(); > o.id = Guid.NewGuid(); > > m.Orders.Add(o); > > string data = Serialization.GetSerializedXml<Master>(m); > } > > public static class Serialization { > > /// <summary> > /// Builds the Serialized xml that represents the specified object > /// </summary> > /// <returns></returns> > public static string GetSerializedXml<ItemType>(ItemType item) { > //Serialze this object and send to the webservice. > XmlSerializer xs = new XmlSerializer(typeof(ItemType)); > StringWriter sw = new StringWriter(); > xs.Serialize(sw, item); > return sw.ToString(); > } > > } > > public class Orders : List<Order> { > [System.Xml.Serialization.XmlAttribute("testAttribute")] > public string TestAttribute; > } > > public class Order { > [System.Xml.Serialization.XmlAttribute("id")] > public Guid id; > } > > public class Master { > > public Master() { > _orders = new Orders(); > } > > private Orders _orders; > [System.Xml.Serialization.XmlArray("orders"), > System.Xml.Serialization.XmlArrayItem("order", >typeof(Order))] > public Orders Orders { > get { return _orders; } > set { _orders = value; } > } > > } J. Merrill / Analytical Software Corp =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
