public void TestSerializeCollectionWithXmlElementAttribute() { // the rule is: // if no type is specified or the specified type // matches the contents of the collection, // serialize each element in an element named after the member. // if the type does not match, or matches the collection itself, // create a base wrapping element for the member, and then // wrap each collection item in its own wrapping element based on type. XmlAttributeOverrides overrides = new XmlAttributeOverrides(); XmlAttributes attr = new XmlAttributes(); XmlElementAttribute element = new XmlElementAttribute(); attr.XmlElements.Add(element); overrides.Add(typeof(StringCollectionContainer), "Messages", attr); // empty collection & no type info in XmlElementAttribute StringCollectionContainer container = new StringCollectionContainer(); Serialize(container, overrides); AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText); // non-empty collection & no type info in XmlElementAttribute container.Messages.Add("hello"); Serialize(container, overrides); AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText); // non-empty collection & only type info in XmlElementAttribute element.Type = typeof(StringCollection); Serialize(container, overrides); AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText); // non-empty collection & only type info in XmlElementAttribute element.Type = typeof(string); Serialize(container, overrides); AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText); // two elements container.Messages.Add("goodbye"); element.Type = null; Serialize(container, overrides); AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText); }
public void TestSerializePlainContainer() { StringCollectionContainer container = new StringCollectionContainer(); Serialize(container); AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText); container.Messages.Add("hello"); container.Messages.Add("goodbye"); Serialize(container); AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText); }