Parse() public static method

public static Parse ( XmlDocument xdoc, Type valueType, MappingAction action, Type &parsedType, Type &parsedArrayType ) : object
xdoc System.Xml.XmlDocument
valueType System.Type
action MappingAction
parsedType System.Type
parsedArrayType System.Type
return object
Example #1
0
        public void Base64_ZeroLengthEmptyTag()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
        <value><base64 /></value>";
            object obj = Utils.Parse(xml, typeof(object), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is byte[], "result is array of byte");
            byte[] ret = obj as byte[];
            Assert.AreEqual(0, ret.Length);
        }
Example #2
0
        public void XmlRpcDateTime_XmlRpcDateTimeType()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
        <value><dateTime.iso8601>20020706T11:25:37</dateTime.iso8601></value>";
            object obj = Utils.Parse(xml, typeof(DateTime?), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.AreEqual(
                new DateTime?(new DateTime(2002, 7, 6, 11, 25, 37)),
                (DateTime?)obj);
        }
Example #3
0
        public void Struct1_AllExist()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value><struct>
<member><name>mi</name><value><i4>12345</i4></value></member>
<member><name>ms</name><value><string>Test String</string></value></member>
<member><name>mb</name><value><boolean>1</boolean></value></member>
<member><name>md</name><value><double>1234.567</double></value></member>
<member><name>mdt</name><value><dateTime.iso8601>20020707T11:25:37</dateTime.iso8601></value></member>
<member><name>mb64</name><value><base64>AQIDBAUGBwg=</base64></value></member>
<member><name>ma</name><value><array><data><value><i4>1</i4></value><value><i4>2</i4></value></data></array></value></member>
<member><name>xi</name><value><i4>23456</i4></value></member>
<member><name>xb</name><value><boolean>1</boolean></value></member>
<member><name>xd</name><value><double>2345.678</double></value></member>
<member><name>xdt</name><value><dateTime.iso8601>20030808T11:25:37</dateTime.iso8601></value></member>
<member><name>xstr</name><value><struct><member><name>key3</name><value><string>test</string></value></member></struct></value></member>"
#if !FX1_0
                         +
                         @"
<member><name>nxi</name><value><i4>34567</i4></value></member>
<member><name>nxb</name><value><boolean>1</boolean></value></member>
<member><name>nxd</name><value><double>3456.789</double></value></member>
<member><name>nxdt</name><value><dateTime.iso8601>20040909T11:25:37</dateTime.iso8601></value></member>
<member><name>nxstr</name><value><struct><member><name>x</name><value><i4>1234</i4></value></member></struct></value></member>
"
#endif
                         +
                         @"</struct></value>";
            object obj = Utils.Parse(xml, typeof(Struct1), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is Struct1, "obj is Struct1");
            Assert.AreEqual(12345, ((Struct1)obj).mi, "int member");
            Assert.AreEqual("Test String", ((Struct1)obj).ms, "string member");
            Assert.AreEqual(true, ((Struct1)obj).mb, "boolean member");
            Assert.AreEqual(1234.567, ((Struct1)obj).md, "double member");
            Assert.AreEqual(new DateTime(2002, 7, 7, 11, 25, 37), ((Struct1)obj).mdt, "dateTime member");
            // TODO:      Assert.AreEqual(null, ((Struct1)obj).mb64, "base64 member");
            // TODO:       Assert.AreEqual(null, ((Struct1)obj).ma, "array member");
            Assert.That(((Struct1)obj).xi == 23456, Is.True, "XmlRpcInt member");
            Assert.IsTrue(true == ((Struct1)obj).xb);
            Assert.IsTrue(2345.678 == ((Struct1)obj).xd);
            Assert.IsTrue(new DateTime(2003, 8, 8, 11, 25, 37).Equals(((Struct1)obj).xdt));
#if !FX1_0
            Assert.AreEqual(34567, ((Struct1)obj).nxi, "int? member");
            Assert.AreEqual(true, ((Struct1)obj).nxb, "bool? member");
            Assert.AreEqual(3456.789, ((Struct1)obj).nxd, "double? member");
            Assert.AreEqual(new DateTime(2004, 9, 9, 11, 25, 37), ((Struct1)obj).nxdt, "DateTime? member");
            Assert.AreEqual(1234, ((ChildStruct)((Struct1)obj).nxstr).x);
#endif
        }
Example #4
0
        public void DateTime_Empty_Standard()
        {
            Type               parsedType, parsedArrayType;
            string             xml        = @"<?xml version=""1.0"" ?>
        <value><dateTime.iso8601></dateTime.iso8601></value>";
            XmlRpcDeserializer serializer = new XmlRpcDeserializer();

            serializer.NonStandard = XmlRpcNonStandard.MapEmptyDateTimeToMinValue;
            object obj = Utils.Parse(xml, typeof(DateTime), MappingAction.Error, serializer,
                                     out parsedType, out parsedArrayType);

            Assert.AreEqual(DateTime.MinValue, (DateTime)obj);
        }
Example #5
0
        public void Issue72()
        {
            Type               parsedType, parsedArrayType;
            string             xml        = @"<?xml version=""1.0"" ?>
        <value><dateTime.iso8601>20090209T22:20:01+01:00</dateTime.iso8601></value>";
            XmlRpcDeserializer serializer = new XmlRpcDeserializer();

            serializer.NonStandard = XmlRpcNonStandard.AllowNonStandardDateTime;
            object obj = Utils.Parse(xml, typeof(DateTime), MappingAction.Error,
                                     serializer, out parsedType, out parsedArrayType);

            Assert.AreEqual(new DateTime(2009, 2, 9, 21, 20, 01),
                            (DateTime)obj);
        }
Example #6
0
        public void DateTime_ROCA()
        {
            Type               parsedType, parsedArrayType;
            string             xml        = @"<?xml version=""1.0"" ?>
        <value><dateTime.iso8601>2002-07-06T11:25:37</dateTime.iso8601></value>";
            XmlRpcDeserializer serializer = new XmlRpcDeserializer();

            serializer.NonStandard = XmlRpcNonStandard.AllowNonStandardDateTime;
            object obj = Utils.Parse(xml, typeof(DateTime), MappingAction.Error, serializer,
                                     out parsedType, out parsedArrayType);

            Assert.AreEqual(new DateTime(2002, 7, 6, 11, 25, 37),
                            (DateTime)obj);
        }
Example #7
0
        public void DateTime_TZMinus01Colon30()
        {
            // yyyyMMddThh:mm:ssZ+00
            Type               parsedType, parsedArrayType;
            string             xml        = @"<?xml version=""1.0"" ?>
        <value><dateTime.iso8601>20020706T09:55:37-01:30</dateTime.iso8601></value>";
            XmlRpcDeserializer serializer = new XmlRpcDeserializer();

            serializer.NonStandard = XmlRpcNonStandard.AllowNonStandardDateTime;
            object obj = Utils.Parse(xml, typeof(DateTime), MappingAction.Error,
                                     serializer, out parsedType, out parsedArrayType);

            Assert.AreEqual(new DateTime(2002, 7, 6, 11, 25, 37),
                            (DateTime)obj);
        }
Example #8
0
        public void NameEmptyString()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name/>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, null, MappingAction.Error,
                                     out parsedType, out parsedArrayType);
        }
Example #9
0
        public void IgnoreNonSerialized()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>y</name>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(Struct4), MappingAction.Error,
                                     out parsedType, out parsedArrayType);
        }
Example #10
0
        public void NoInnerStructOverrideIgnoreError()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
      <value>
        <struct>
          <member>
            <name>mstruct</name>
            <value><struct></struct></value>
          </member>
        </struct>
      </value>";

            Assert.That(() => Utils.Parse(xml, typeof(StructOuter1), MappingAction.Error, out parsedType, out parsedArrayType), Throws.TypeOf <XmlRpcTypeMismatchException>());
        }
Example #11
0
        public void Array()
        {
            object[]    testary = new Object[] { 12, "Egypt", false };
            XmlDocument xdoc    = Utils.Serialize("SerializeTest.testArray",
                                                  testary,
                                                  Encoding.UTF8, MappingAction.Ignore);
            Type   parsedType, parsedArrayType;
            object obj = Utils.Parse(xdoc, null, MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is object[], "result is array of object");
            object[] ret = obj as object[];
            Assert.AreEqual(12, ret[0]);
            Assert.AreEqual("Egypt", ret[1]);
            Assert.AreEqual(false, ret[2]);
        }
Example #12
0
   public void NoInnerStructOverrideErrorIgnore()
   {
       Type   parsedType, parsedArrayType;
       string xml = @"<?xml version=""1.0"" ?>
 <value>
   <struct>
     <member>
       <name>mstruct</name>
       <value><struct></struct></value>
     </member>
   </struct>
 </value>";
       object obj = Utils.Parse(xml, typeof(StructOuter2),
                                MappingAction.Ignore,
                                out parsedType, out parsedArrayType);
   }
Example #13
0
        public void NameEmptyString()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name/>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";

            Assert.That(() => Utils.Parse(xml, null, MappingAction.Error,
                                          out parsedType, out parsedArrayType), Throws.TypeOf <XmlRpcInvalidXmlRpcException>());
        }
Example #14
0
        public void Base64_ObjectType()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
        <value><base64>eW91IGNhbid0IHJlYWQgdGhpcyE=</base64></value>";
            object obj = Utils.Parse(xml, typeof(object), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is byte[], "result is array of byte");
            byte[] ret = obj as byte[];
            Assert.IsTrue(ret.Length == testb.Length);
            for (int i = 0; i < testb.Length; i++)
            {
                Assert.IsTrue(testb[i] == ret[i]);
            }
        }
        public void NoInnerStructOverrideIgnoreError()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
      <value>
        <struct>
          <member>
            <name>mstruct</name>
            <value><struct></struct></value>
          </member>
        </struct>
      </value>";
            object obj = Utils.Parse(xml, typeof(StructOuter1), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.Fail("didn't detect missing members");
        }
Example #16
0
        public void NoInnerStructOverrideIgnoreError()
        {
            Type parsedType, parsedArrayType;
            var  xml = @"<?xml version=""1.0"" ?>
      <value>
        <struct>
          <member>
            <name>mstruct</name>
            <value><struct></struct></value>
          </member>
        </struct>
      </value>";

            Assert.Throws <XmlRpcTypeMismatchException>(() => Utils.Parse(xml, typeof(StructOuter1), MappingAction.Error,
                                                                          out parsedType, out parsedArrayType));
            // Assert.Fail("didn't detect missing members");
        }
Example #17
0
        public void NonSerializedNonXmlRpcType()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>y</name>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(Struct4), MappingAction.Error,
                                     out parsedType, out parsedArrayType);
            Struct4 ret = (Struct4)obj;

            Assert.AreEqual(0, ret.x);
            Assert.AreEqual(18, ret.y);
        }
Example #18
0
        public void MultiDimArray()
        {
            int[,] myArray = new int[, ] {
                { 1, 2 }, { 3, 4 }
            };
            XmlDocument xdoc = Utils.Serialize("SerializeTest.testMultiDimArray",
                                               myArray,
                                               Encoding.UTF8, MappingAction.Ignore);
            Type   parsedType, parsedArrayType;
            object obj = Utils.Parse(xdoc, typeof(int[, ]), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is int[, ], "result is 2 dim array of int");
            int[,] ret = obj as int[, ];
            Assert.AreEqual(1, ret[0, 0]);
            Assert.AreEqual(2, ret[0, 1]);
            Assert.AreEqual(3, ret[1, 0]);
            Assert.AreEqual(4, ret[1, 1]);
        }
Example #19
0
        public void UnexpectedMember()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>x</name>
      <value><i4>12</i4></value>
    </member>
    <member>
      <name>y</name>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(Struct5), MappingAction.Error,
                                     out parsedType, out parsedArrayType);
        }
Example #20
0
        public void PropertyXmlRpcName()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>IntField</name>
      <value><i4>18</i4></value>
    </member>
    <member>
      <name>IntProperty</name>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(Struct3), MappingAction.Error,
                                     out parsedType, out parsedArrayType);
        }
Example #21
0
        public void XmlRpcStruct()
        {
            XmlRpcStruct xmlRpcStruct = new XmlRpcStruct();

            xmlRpcStruct["mi"] = 34567;
            xmlRpcStruct["ms"] = "another test string";

            XmlDocument xdoc = Utils.Serialize("SerializeTest.testXmlRpcStruct",
                                               xmlRpcStruct, Encoding.UTF8, MappingAction.Ignore);
            Type   parsedType, parsedArrayType;
            object obj = Utils.Parse(xdoc, typeof(XmlRpcStruct), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is XmlRpcStruct, "result is XmlRpcStruct");
            xmlRpcStruct = obj as XmlRpcStruct;
            Assert.IsTrue(xmlRpcStruct["mi"] is int);
            Assert.AreEqual((int)xmlRpcStruct["mi"], 34567);
            Assert.IsTrue(xmlRpcStruct["ms"] is string);
            Assert.AreEqual((string)xmlRpcStruct["ms"], "another test string");
        }
Example #22
0
        public void NonSerializedInStruct()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>x</name>
      <value><i4>12</i4></value>
    </member>
    <member>
      <name>y</name>
      <value><i4>18</i4></value>
    </member>
  </struct>
</value>";

            Assert.That(
                () => Utils.Parse(xml, typeof(Struct4), MappingAction.Error, out parsedType, out parsedArrayType),
                Throws.TypeOf <XmlRpcNonSerializedMember>());
        }
Example #23
0
        public void XmlRpcStructArray()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <array>
    <data>
      <value>
        <struct>
          <member>
            <name>mi</name>
            <value><i4>18</i4></value>
          </member>
        </struct>
      </value>
      <value>
        <struct>
          <member>
            <name>mi</name>
            <value><i4>28</i4></value>
          </member>
        </struct>
      </value>
    </data>
  </array>
</value>";

            object obj = Utils.Parse(xml, null, MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.AreEqual(obj.GetType(), typeof(XmlRpcStruct[]));
            object[] objarray = (object[])obj;
            Assert.AreEqual(objarray[0].GetType(), typeof(XmlRpcStruct));
            Assert.AreEqual(objarray[1].GetType(), typeof(XmlRpcStruct));
            XmlRpcStruct xstruct1 = objarray[0] as XmlRpcStruct;
            XmlRpcStruct xstruct2 = objarray[1] as XmlRpcStruct;

            Assert.AreEqual(xstruct1["mi"], 18);
            Assert.AreEqual(xstruct2["mi"], 28);
        }
Example #24
0
        public void JaggedArray()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
 <value>
   <array>
     <data>
       <value>
         <array>
           <data>
             <value>
               <i4>1213028</i4>
             </value>
             <value>
               <string>products</string>
             </value>
           </data>
         </array>
       </value>
       <value>
         <array>
           <data>
             <value>
               <i4>666</i4>
             </value>
           </data>
         </array>
       </value>
     </data>
   </array>
 </value>";
            object obj = Utils.Parse(xml, typeof(object[][]), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is object[][]);
            object[][] ret = (object[][])obj;
            Assert.AreEqual(1213028, ret[0][0]);
            Assert.AreEqual("products", ret[0][1]);
            Assert.AreEqual(666, ret[1][0]);
        }
        public void HomogArray_ObjectType()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <array>
    <data>
      <value><i4>12</i4></value>
      <value><i4>13</i4></value>
      <value><i4>14</i4></value>
    </data>
  </array>
</value>";
            object obj = Utils.Parse(xml, typeof(object), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is int[], "result is array of int");
            int[] ret = obj as int[];
            Assert.AreEqual(12, ret[0]);
            Assert.AreEqual(13, ret[1]);
            Assert.AreEqual(14, ret[2]);
        }
Example #26
0
        public void DateTimeWarekiCalendar()
        {
            CultureInfo oldci = Thread.CurrentThread.CurrentCulture;

            try
            {
                CultureInfo ci = new CultureInfo("ja-JP");
                Thread.CurrentThread.CurrentCulture = ci;
                ci.DateTimeFormat.Calendar          = new JapaneseCalendar();
                XmlDocument xdoc = Utils.Serialize("SerializeTest.testDateTime",
                                                   new DateTime(2002, 7, 6, 11, 25, 37),
                                                   Encoding.UTF8, MappingAction.Ignore);
                Type   parsedType, parsedArrayType;
                object obj = Utils.Parse(xdoc, null, MappingAction.Error,
                                         out parsedType, out parsedArrayType);
                Assert.AreEqual(new DateTime(2002, 7, 6, 11, 25, 37), obj);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = oldci;
            }
        }
Example #27
0
        public void Base64()
        {
            byte[] testb = new Byte[]
            {
                121, 111, 117, 32, 99, 97, 110, 39, 116, 32, 114, 101, 97, 100,
                32, 116, 104, 105, 115, 33
            };
            XmlDocument xdoc = Utils.Serialize("SerializeTest.testBase64",
                                               testb,
                                               Encoding.UTF8, MappingAction.Ignore);
            Type   parsedType, parsedArrayType;
            object obj = Utils.Parse(xdoc, null, MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is byte[], "result is array of byte");
            byte[] ret = obj as byte[];
            Assert.IsTrue(ret.Length == testb.Length);
            for (int i = 0; i < testb.Length; i++)
            {
                Assert.IsTrue(testb[i] == ret[i]);
            }
        }
        public void MixedArray_ObjectType()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <array>
    <data>
      <value><i4>12</i4></value>
      <value><string>Egypt</string></value>
      <value><boolean>0</boolean></value>
    </data>
  </array>
</value>";
            object obj = Utils.Parse(xml, typeof(object), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsTrue(obj is object[], "result is array of object");
            object[] ret = obj as object[];
            Assert.AreEqual(12, ret[0]);
            Assert.AreEqual("Egypt", ret[1]);
            Assert.AreEqual(false, ret[2]);
        }
Example #29
0
        public void RecursiveMemberTest()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>Level</name>
      <value>1</value>
    </member>
    <member>
      <name>childExample</name>
      <value>
        <struct>
          <member>
            <name>Level</name>
            <value>2</value>
          </member>
          <member>
            <name>childExample</name>
            <value>
              <struct>
                <member>
                  <name>Level</name>
                  <value>3</value>
                </member>
              </struct>
            </value>
          </member>
        </struct>
      </value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(RecursiveMember), MappingAction.Error,
                                     out parsedType, out parsedArrayType);
            RecursiveMember ret = (RecursiveMember)obj;
        }
Example #30
0
        public void XmlRpcStructOrder()
        {
            Type   parsedType, parsedArrayType;
            string xml = @"<?xml version=""1.0"" ?>
<value>
  <struct>
    <member>
      <name>a</name>
      <value><i4>1</i4></value>
    </member>
    <member>
      <name>c</name>
      <value><i4>3</i4></value>
    </member>
    <member>
      <name>b</name>
      <value><i4>2</i4></value>
    </member>
  </struct>
</value>";
            object obj = Utils.Parse(xml, typeof(XmlRpcStruct), MappingAction.Error,
                                     out parsedType, out parsedArrayType);

            Assert.IsInstanceOfType(typeof(XmlRpcStruct), obj);
            XmlRpcStruct          strct       = obj as XmlRpcStruct;
            IDictionaryEnumerator denumerator = strct.GetEnumerator();

            denumerator.MoveNext();
            Assert.AreEqual("a", denumerator.Key);
            Assert.AreEqual(1, denumerator.Value);
            denumerator.MoveNext();
            Assert.AreEqual("c", denumerator.Key);
            Assert.AreEqual(3, denumerator.Value);
            denumerator.MoveNext();
            Assert.AreEqual("b", denumerator.Key);
            Assert.AreEqual(2, denumerator.Value);
        }