using System.Xml.Schema;
using System.Xml.Serialization;
using System.Reflection;
-#if NET_2_0
using System.Collections.Generic;
-#endif
using NUnit.Framework;
}
// test constructors
-#if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
[Test]
+ [ExpectedException (typeof (ArgumentNullException))]
public void TestConstructor()
{
XmlSerializer ser = new XmlSerializer (null, "");
}
-#else
-#endif
// test basic types ////////////////////////////////////////////////////////
[Test]
}
[Test]
- [Category ("NotWorking")]
+ [Category ("MobileNotWorking")]
public void TestSerializeEnumeration_FromValue_Encoded ()
{
SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
}
[Test]
- [Category ("NotWorking")]
+ [Category ("MobileNotWorking")]
public void TestSerializeEnumDefaultValue_Encoded ()
{
SerializeEncoded (new EnumDefaultValue ());
[Test]
public void TestSerializeEnumDefaultValue_InvalidValue2 ()
{
-#if NET_2_0
try {
Serialize (5, typeof (EnumDefaultValue));
Assert.Fail ("#1");
Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
}
-#else
- Serialize (5, typeof (EnumDefaultValue));
- Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
-#endif
}
[Test]
public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
{
-#if NET_2_0
try {
Serialize (new EnumDefaultValueNF ());
Assert.Fail ("#1");
Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
}
-#else
- Serialize (new EnumDefaultValueNF ());
- Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
-#endif
}
[Test]
public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
{
-#if NET_2_0
try {
Serialize (15, typeof (EnumDefaultValueNF));
Assert.Fail ("#1");
Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
}
-#else
- Serialize (15, typeof (EnumDefaultValueNF));
- Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
-#endif
}
[Test]
f.Modifiers3 = (MapModifiers) 0;
f.Modifiers4 = (MapModifiers) 888;
f.Modifiers5 = (MapModifiers) 999;
-#if NET_2_0
try {
Serialize (f, typeof (Field));
Assert.Fail ("#E1");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
Assert.IsNull (ex.InnerException.InnerException, "#E9");
}
-#else
- Serialize (f, typeof (Field));
- Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
- "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
- " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
- " modifiers4='888' modifiers5='999' names='a b' />",
- XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
-#endif
}
[Test]
- [Category ("NotDotNet")] // MS bug
+ [Category ("NotWorking")] // MS bug
public void TestSerializeField_Encoded ()
{
Field_Encoded f = new Field_Encoded ();
SerializeEncoded (f, typeof (Field_Encoded));
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>" +
-#if NET_2_0
- "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
- " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
- " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
-#else
"<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
" flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
" modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
-#endif
XmlSchema.Namespace, XmlSchema.InstanceNamespace),
sw.GetStringBuilder ().ToString (), "#A");
SerializeEncoded (f, typeof (Field_Encoded));
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>" +
-#if NET_2_0
- "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
- " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
- " xmlns:q1='some:urn' />",
-#else
"<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
" flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
" xmlns:q1='some:urn' />",
-#endif
XmlSchema.Namespace, XmlSchema.InstanceNamespace),
sw.GetStringBuilder ().ToString (), "#B");
SerializeEncoded (f, typeof (Field_Encoded));
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>" +
-#if NET_2_0
- "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
- " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
- " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
- " xmlns:q1='some:urn' />",
-#else
"<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
" flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
" modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
" xmlns:q1='some:urn' />",
-#endif
XmlSchema.Namespace, XmlSchema.InstanceNamespace),
sw.GetStringBuilder ().ToString (), "#C");
f.Modifiers3 = (MapModifiers) 0;
f.Modifiers4 = (MapModifiers) 888;
f.Modifiers5 = (MapModifiers) 999;
-#if NET_2_0
try {
-#endif
SerializeEncoded (f, typeof (Field_Encoded));
-#if NET_2_0
Assert.Fail ("#D1");
} catch (InvalidOperationException ex) {
// There was an error generating the XML document
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
Assert.IsNull (ex.InnerException.InnerException, "#D9");
}
-#else
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "<?xml version='1.0' encoding='utf-16'?>" +
- "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
- " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
- " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
- XmlSchema.Namespace, XmlSchema.InstanceNamespace),
- sw.GetStringBuilder ().ToString (), "#D");
-#endif
}
[Test]
[Test]
public void TestSerializeZeroFlagEnum_InvalidValue ()
{
-#if NET_2_0
try {
Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
Assert.Fail ("#1");
Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
}
-#else
- Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
- Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
-#endif
}
[Test]
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan off = tz.GetUtcOffset (d);
string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
-#if NET_2_0
Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
-#else
- Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
-#endif
}
/*
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");
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");
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");
}
-
+
+ [Test]
+ public void TestRoundTripSerializeOptionalValueTypeContainer ()
+ {
+ var source = new OptionalValueTypeContainer ();
+ source.IsEmpty = true;
+ source.IsEmptySpecified = true;
+ var ser = new XmlSerializer (typeof (OptionalValueTypeContainer));
+ string xml;
+ using (var t = new StringWriter ()) {
+ ser.Serialize (t, source);
+ xml = t.ToString();
+ }
+ using (var s = new StringReader (xml)) {
+ var obj = (OptionalValueTypeContainer) ser.Deserialize(s);
+ Assert.AreEqual (source.IsEmpty, obj.IsEmpty, "#1");
+ Assert.AreEqual (source.IsEmptySpecified, obj.IsEmptySpecified, "#2");
+ }
+ }
+
[Test]
public void TestSerializePlainContainer ()
{
}
[Test]
- [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
public void TestSerializeDefaultValueAttribute_Encoded ()
{
SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
// technically XmlSchemaForm.None has an XmlIgnore attribute,
// but it is not being serialized as a member.
-#if NET_2_0
try {
Serialize (XmlSchemaForm.None);
Assert.Fail ("#1");
Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
}
-#else
- Serialize (XmlSchemaForm.None);
- Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
-#endif
}
[Test]
[Test]
[ExpectedException (typeof (InvalidOperationException))]
- [Category ("NotWorking")]
+ [Category ("MobileNotWorking")]
public void TestSerializeXmlNodeArrayIncludesAttribute ()
{
XmlDocument doc = new XmlDocument ();
Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
}
-#if NET_2_0
[Test]
[ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
public void TestSerializeGenericListOfNode ()
Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
}
-#endif
[Test]
public void TestSerializeXmlDocument ()
{
Assert.AreEqual (Infoset (res), WriterText);
}
+ [Test] // Covers #36829
+ public void TestSubclassElementList ()
+ {
+ var o = new SubclassTestList () { Items = new List<object> () { new SubclassTestSub () } };
+ Serialize (o);
+
+ string res = "<SubclassTestList xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
+ res += "<b xsi:type=\"SubclassTestSub\"/></SubclassTestList>";
+ Assert.AreEqual (Infoset (res), WriterText);
+ }
+
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void TestArrayAttributeWithWrongDataType ()
Serialize (new PrimitiveTypesContainer ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>" +
-#if NET_2_0
"<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
-#else
- "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
-#endif
"<Number>2004</Number>" +
"<Name>some name</Name>" +
"<Index>56</Index>" +
SerializeEncoded (new PrimitiveTypesContainer ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>" +
-#if NET_2_0
"<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
-#else
- "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
-#endif
"<Number xsi:type='xsd:int'>2004</Number>" +
"<Name xsi:type='xsd:string'>some name</Name>" +
"<Index xsi:type='xsd:unsignedByte'>56</Index>" +
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
+ [Category ("MobileNotWorking")]
public void XmlArrayAttributeUnqualifiedWithNamespace ()
{
new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
}
[Test]
- [ExpectedException (typeof (InvalidOperationException))]
+ [Category ("MobileNotWorking")]
public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
{
new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
}
[Test]
-#if !NET_2_0
- [ExpectedException (typeof (ApplicationException))]
-#endif
public void SerializeErrorneousIXmlSerializable ()
{
Serialize (new ErrorneousGetSchema ());
Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
}
-#if NET_2_0
+ [Test]
public void DateTimeRoundtrip ()
{
// bug #337729
DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
Assert.AreEqual (DateTimeKind.Utc, d.Kind);
}
-#endif
[Test]
public void SupportIXmlSerializableImplicitlyConvertible ()
#region GenericsSeralizationTests
-#if NET_2_0
[Test]
public void TestSerializeGenSimpleClassString ()
{
Assert.AreEqual(WriterText, expected_text, WriterText);
}
-#endif
#endregion //GenericsSeralizationTests
+ #region XmlInclude on abstract class tests (Bug #18558)
+ [Test]
+ public void TestSerializeIntermediateType ()
+ {
+ string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlIntermediateType intermediate=\"false\"/></ContainerTypeForTest>";
+ var obj = new ContainerTypeForTest();
+ obj.MemberToUseInclude = new IntermediateTypeForTest ();
+ Serialize (obj);
+ Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
+ }
+ [Test]
+ public void TestSerializeSecondType ()
+ {
+ string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlSecondType intermediate=\"false\"/></ContainerTypeForTest>";
+ var obj = new ContainerTypeForTest();
+ obj.MemberToUseInclude = new SecondDerivedTypeForTest ();
+ Serialize (obj);
+ Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
+ }
+ #endregion
public class XmlArrayOnInt
{
[XmlArray]
public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
}
-#if NET_2_0
public class Bug80759
{
public string Test;
}
}
-#endif
void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
{
}
[Test]
- [Category("NotDotNet")]
+ [Category("NotWorking")]
public void ShouldSerializeGeneric ()
{
var ser = new XmlSerializer (typeof (ClassWithShouldSerializeGeneric));
generatorFallback.SetValue (null, generatorFallbackOld);
}
}
+
+#region XmlInclude on abstract class test classes
+
+ [XmlType]
+ public class ContainerTypeForTest
+ {
+ [XmlElement ("XmlSecondType", typeof (SecondDerivedTypeForTest))]
+ [XmlElement ("XmlIntermediateType", typeof (IntermediateTypeForTest))]
+ [XmlElement ("XmlFirstType", typeof (FirstDerivedTypeForTest))]
+ public AbstractTypeForTest MemberToUseInclude { get; set; }
+ }
+
+ [XmlInclude (typeof (SecondDerivedTypeForTest))]
+ [XmlInclude (typeof (IntermediateTypeForTest))]
+ [XmlInclude (typeof (FirstDerivedTypeForTest))]
+ public abstract class AbstractTypeForTest
+ {
+ }
+
+ public class IntermediateTypeForTest : AbstractTypeForTest
+ {
+ [XmlAttribute (AttributeName = "intermediate")]
+ public bool IntermediateMember { get; set; }
+ }
+
+ public class FirstDerivedTypeForTest : AbstractTypeForTest
+ {
+ public string FirstMember { get; set; }
+ }
+
+ public class SecondDerivedTypeForTest : IntermediateTypeForTest
+ {
+ public string SecondMember { get; set; }
+ }
+#endregion
}