public void TestSerializeDefaultValueAttribute () { XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr = new XmlAttributes (); string defaultValueInstance = "nothing"; attr.XmlDefaultValue = defaultValueInstance; overrides.Add (typeof (SimpleClass), "something", attr); // use the default SimpleClass simple = new SimpleClass (); Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1"); // same value as default simple.something = defaultValueInstance; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2"); // some other value simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3"); overrides = new XmlAttributeOverrides (); attr = new XmlAttributes (); attr.XmlAttribute = new XmlAttributeAttribute (); attr.XmlDefaultValue = defaultValueInstance; overrides.Add (typeof (SimpleClass), "something", attr); // use the default simple = new SimpleClass (); Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1"); // same value as default simple.something = defaultValueInstance; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2"); // some other value simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3"); overrides = new XmlAttributeOverrides (); attr = new XmlAttributes (); attr.XmlAttribute = new XmlAttributeAttribute ("flagenc"); overrides.Add (typeof (TestDefault), "flagencoded", attr); // use the default TestDefault testDefault = new TestDefault (); Serialize (testDefault); Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1"); // use the default with overrides Serialize (testDefault, overrides); Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2"); overrides = new XmlAttributeOverrides (); attr = new XmlAttributes (); attr.XmlAttribute = new XmlAttributeAttribute ("flagenc"); attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again overrides.Add (typeof (TestDefault), "flagencoded", attr); // use the default with overrides Serialize (testDefault, overrides); Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3"); // use the default with overrides and default namspace Serialize (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4"); // non-default values testDefault.strDefault = "Some Text"; testDefault.boolT = false; testDefault.boolF = true; testDefault.decimalval = 20m; testDefault.flag = FlagEnum.e2; testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1; Serialize (testDefault); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault>Some Text</strDefault>" + " <boolT>false</boolT>" + " <boolF>true</boolF>" + " <decimalval>20</decimalval>" + " <flag>two</flag>" + " <flagencoded>e1 e2</flagencoded>" + "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C5"); Serialize (testDefault, overrides); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault>Some Text</strDefault>" + " <boolT>false</boolT>" + " <boolF>true</boolF>" + " <decimalval>20</decimalval>" + " <flag>two</flag>" + "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C6"); Serialize (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault>Some Text</strDefault>" + " <boolT>false</boolT>" + " <boolF>true</boolF>" + " <decimalval>20</decimalval>" + " <flag>two</flag>" + "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C7"); attr = new XmlAttributes (); XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum"); xmlType.Namespace = "yetanother:urn"; attr.XmlType = xmlType; overrides.Add (typeof (FlagEnum_Encoded), attr); Serialize (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault>Some Text</strDefault>" + " <boolT>false</boolT>" + " <boolF>true</boolF>" + " <decimalval>20</decimalval>" + " <flag>two</flag>" + "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C8"); attr = new XmlAttributes (); attr.XmlType = new XmlTypeAttribute ("testDefault"); overrides.Add (typeof (TestDefault), attr); Serialize (testDefault, overrides, AnotherNamespace); Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture, "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" + " <strDefault>Some Text</strDefault>" + " <boolT>false</boolT>" + " <boolF>true</boolF>" + " <decimalval>20</decimalval>" + " <flag>two</flag>" + "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace)), WriterText, "#C9"); }
public void SupportIXmlSerializableImplicitlyConvertible () { XmlAttributes attrs = new XmlAttributes (); XmlElementAttribute attr = new XmlElementAttribute (); attr.ElementName = "XmlSerializable"; attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable); attrs.XmlElements.Add (attr); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides (); attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs); XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible (); new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x); }
public void TestSerializeXmlElementAttribute () { XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr = new XmlAttributes (); XmlElementAttribute element = new XmlElementAttribute (); attr.XmlElements.Add (element); overrides.Add (typeof (SimpleClass), "something", attr); // null SimpleClass simple = new SimpleClass (); ; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1"); // not null simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2"); //ElementName element.ElementName = "saying"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3"); //IsNullable element.IsNullable = false; simple.something = null; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4"); element.IsNullable = true; simple.something = null; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5"); //Namespace element.ElementName = null; element.IsNullable = false; element.Namespace = "some:urn"; simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6"); //FIXME DataType //FIXME Form //FIXME Type }
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); Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1"); // non-empty collection & no type info in XmlElementAttribute container.Messages.Add ("hello"); Serialize (container, overrides); Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2"); // non-empty collection & only type info in XmlElementAttribute element.Type = typeof (StringCollection); Serialize (container, overrides); Assert.AreEqual (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, "#3"); // non-empty collection & only type info in XmlElementAttribute element.Type = typeof (string); Serialize (container, overrides); Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4"); // two elements container.Messages.Add ("goodbye"); element.Type = null; Serialize (container, overrides); Assert.AreEqual (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, "#5"); }
public void TestSerializeXmlAttributeAttribute () { // null XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr = new XmlAttributes (); attr.XmlAttribute = new XmlAttributeAttribute (); overrides.Add (typeof (SimpleClass), "something", attr); SimpleClass simple = new SimpleClass (); ; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1"); // regular simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2"); // AttributeName attr.XmlAttribute.AttributeName = "somethingelse"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3"); // Type // FIXME this should work, shouldnt it? // attr.XmlAttribute.Type = typeof(string); // Serialize(simple, overrides); // Assert(WriterText.EndsWith(" something='hello' />")); // Namespace attr.XmlAttribute.Namespace = "some:urn"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4"); // FIXME DataType // FIXME XmlSchemaForm Form // FIXME write XmlQualifiedName as attribute }
public void TestSerializeXmlRootAttributeOnMember () { // nested root XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes childAttr = new XmlAttributes (); childAttr.XmlRoot = new XmlRootAttribute ("simple"); overrides.Add (typeof (SimpleClass), childAttr); XmlAttributes attr = new XmlAttributes (); attr.XmlRoot = new XmlRootAttribute ("simple"); overrides.Add (typeof (ClassArrayContainer), attr); ClassArrayContainer container = new ClassArrayContainer (); container.items = new SimpleClass[1]; container.items[0] = new SimpleClass (); Serialize (container, overrides); Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText); // FIXME test data type }
public void TestSerializeXmlTextAttribute () { SimpleClass simple = new SimpleClass (); simple.something = "hello"; XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr = new XmlAttributes (); overrides.Add (typeof (SimpleClass), "something", attr); attr.XmlText = new XmlTextAttribute (); Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1"); attr.XmlText = new XmlTextAttribute (typeof (string)); Serialize (simple, overrides); Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2"); try { attr.XmlText = new XmlTextAttribute (typeof (byte[])); Serialize (simple, overrides); Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed"); } catch (InvalidOperationException ex) { // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass' Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2"); Assert.IsNotNull (ex.Message, "#A3"); Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4"); Assert.IsNotNull (ex.InnerException, "#A5"); // there was an error reflecting field 'something'. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6"); Assert.IsNotNull (ex.InnerException.Message, "#A7"); Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8"); Assert.IsNotNull (ex.InnerException.InnerException, "#A9"); // the type for XmlText may not be specified for primitive types. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10"); Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11"); Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12"); } try { attr.XmlText = new XmlTextAttribute (); attr.XmlText.DataType = "sometype"; Serialize (simple, overrides); Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed"); } catch (InvalidOperationException ex) { // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2"); Assert.IsNotNull (ex.Message, "#B3"); Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4"); Assert.IsNotNull (ex.InnerException, "#B5"); // There was an error reflecting field 'something'. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6"); Assert.IsNotNull (ex.InnerException.Message, "#B7"); Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8"); Assert.IsNotNull (ex.InnerException.InnerException, "#B9"); //FIXME /* // There was an error reflecting type 'System.String'. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10"); Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11"); Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12"); Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13"); // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14"); Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15"); Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16"); Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17"); */ } }
public void TestSerializeSimpleClassWithOverrides () { // Also tests XmlIgnore XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr = new XmlAttributes (); attr.XmlIgnore = true; overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr); SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes (); simple.something = "hello"; Serialize (simple, overrides); Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText); }
public void TestSerializeOptionalValueTypeContainer () { XmlAttributeOverrides overrides = new XmlAttributeOverrides (); XmlAttributes attr; OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer (); Serialize (optionalValue); Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" + #if NET_2_0 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />", #else "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />", #endif XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace), sw.ToString (), "#1"); attr = new XmlAttributes (); // remove the DefaultValue attribute on the Flags member overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr); // remove the DefaultValue attribute on the Attributes member overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr); Serialize (optionalValue, overrides); Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" + #if NET_2_0 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" + #else "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" + #endif "<Attributes xmlns='{3}'>one four</Attributes>" + "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace, ANamespace), sw.ToString (), "#2"); optionalValue.FlagsSpecified = true; Serialize (optionalValue, overrides); Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" + #if NET_2_0 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" + #else "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" + #endif "<Attributes xmlns='{3}'>one four</Attributes>" + "<Flags xmlns='{3}'>one</Flags>" + "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace, ANamespace), sw.ToString (), "#3"); }