Exemplo n.º 1
0
        public void TestObject()
        {
            var service = this.GetService();

            var model = new MyItem
            {
                MyGuid      = new Guid("11ba7957-5afb-4b59-9d9b-c06a18cda5c2"),
                MyInt       = 1,
                MyDouble    = 1.5,
                MyString    = "my value",
                MyBool      = true,
                MyEnumValue = MyEnum.Other,
                MyDate      = new DateTime(1990, 12, 12),
                MyObj       = new MyInnerItem {
                    MyInnerString = "my inner value"
                },
                MyList = new List <string> {
                    "a", "b"
                },
                MyArray = new string[] { "y", "z" }
            };

            var xml = "<MyItem><MyGuid>11ba7957-5afb-4b59-9d9b-c06a18cda5c2</MyGuid><MyInt>1</MyInt><MyDouble>1.5</MyDouble><MyString>my value</MyString><MyBool>true</MyBool><MyEnumValue>Other</MyEnumValue><MyDate>12/12/1990 00:00:00</MyDate><MyObj><MyInnerString>my inner value</MyInnerString></MyObj><MyList><String>a</String><String>b</String></MyList><MyArray><String>y</String><String>z</String></MyArray></MyItem>";

            var result = service.ToXmlValue(xml);

            Assert.AreEqual(XmlValueType.Object, result.ValueType);
            Assert.AreEqual("MyItem", result.NodeName);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["MyGuid"].ValueType);
            Assert.AreEqual(model.MyGuid.ToString(), ((XmlString)((XmlObject)result).Values["MyGuid"]).Value);
            Assert.AreEqual("MyGuid", ((XmlString)((XmlObject)result).Values["MyGuid"]).NodeName);

            Assert.AreEqual(XmlValueType.Number, ((XmlObject)result).Values["MyInt"].ValueType);
            Assert.AreEqual(model.MyInt, ((XmlNumber)((XmlObject)result).Values["MyInt"]).Value);
            Assert.AreEqual("MyInt", ((XmlNumber)((XmlObject)result).Values["MyInt"]).NodeName);

            Assert.AreEqual(XmlValueType.Number, ((XmlObject)result).Values["MyDouble"].ValueType);
            Assert.AreEqual(model.MyDouble, ((XmlNumber)((XmlObject)result).Values["MyDouble"]).Value);
            Assert.AreEqual("MyDouble", ((XmlNumber)((XmlObject)result).Values["MyDouble"]).NodeName);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["MyString"].ValueType);
            Assert.AreEqual(model.MyString, ((XmlString)((XmlObject)result).Values["MyString"]).Value);
            Assert.AreEqual("MyString", ((XmlString)((XmlObject)result).Values["MyString"]).NodeName);

            Assert.AreEqual(XmlValueType.Bool, ((XmlObject)result).Values["MyBool"].ValueType);
            Assert.AreEqual(model.MyBool, ((XmlBool)((XmlObject)result).Values["MyBool"]).Value);
            Assert.AreEqual("MyBool", ((XmlBool)((XmlObject)result).Values["MyBool"]).NodeName);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["MyEnumValue"].ValueType);
            Assert.AreEqual("Other", ((XmlString)((XmlObject)result).Values["MyEnumValue"]).Value);
            Assert.AreEqual("MyEnumValue", ((XmlString)((XmlObject)result).Values["MyEnumValue"]).NodeName);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["MyDate"].ValueType);
            Assert.AreEqual(model.MyDate.ToString(), ((XmlString)((XmlObject)result).Values["MyDate"]).Value);
            Assert.AreEqual("MyDate", ((XmlString)((XmlObject)result).Values["MyDate"]).NodeName);

            Assert.AreEqual(XmlValueType.Object, ((XmlObject)result).Values["MyObj"].ValueType);
            Assert.AreEqual("MyObj", ((XmlObject)result).Values["MyObj"].NodeName);

            var myObj = ((XmlObject)result).Values["MyObj"] as XmlObject;

            Assert.AreEqual(XmlValueType.String, myObj.Values["MyInnerString"].ValueType);
            Assert.AreEqual("MyInnerString", myObj.Values["MyInnerString"].NodeName);
            Assert.AreEqual(model.MyObj.MyInnerString, ((XmlString)myObj.Values["MyInnerString"]).Value);

            Assert.AreEqual(XmlValueType.Array, ((XmlObject)result).Values["MyList"].ValueType);
            Assert.AreEqual("MyList", ((XmlObject)result).Values["MyList"].NodeName);

            var myList = ((XmlObject)result).Values["MyList"] as XmlArray;

            Assert.AreEqual("String", myList.Values[0].NodeName);
            Assert.AreEqual(XmlValueType.String, myList.Values[0].ValueType);
            Assert.AreEqual("a", ((XmlString)myList.Values[0]).Value);

            Assert.AreEqual("String", myList.Values[1].NodeName);
            Assert.AreEqual(XmlValueType.String, myList.Values[1].ValueType);
            Assert.AreEqual("b", ((XmlString)myList.Values[1]).Value);

            Assert.AreEqual(XmlValueType.Array, ((XmlObject)result).Values["MyArray"].ValueType);
            Assert.AreEqual("MyArray", ((XmlObject)result).Values["MyArray"].NodeName);

            var myArray = ((XmlObject)result).Values["MyArray"] as XmlArray;

            Assert.AreEqual("String", myArray.Values[0].NodeName);
            Assert.AreEqual(XmlValueType.String, myArray.Values[0].ValueType);
            Assert.AreEqual("y", ((XmlString)myArray.Values[0]).Value);

            Assert.AreEqual("String", myArray.Values[1].NodeName);
            Assert.AreEqual(XmlValueType.String, myArray.Values[1].ValueType);
            Assert.AreEqual("z", ((XmlString)myArray.Values[1]).Value);
        }
        public void TestArrayOfObjects()
        {
            var service = this.GetService();

            var item = new MyItem
            {
                MyGuid      = new Guid("11ba7957-5afb-4b59-9d9b-c06a18cda5c2"),
                MyInt       = 1,
                MyDouble    = 1.5,
                MyString    = "my value 1",
                MyBool      = true,
                MyEnumValue = MyEnum.Other,
                MyDate      = new DateTime(1990, 12, 12),
                MyObj       = new MyInnerItem {
                    MyInnerString = "my inner value 1"
                },
                MyList = new List <string> {
                    "a1", "b1"
                },
                MyArray = new string[] { "y1", "z1" }
            };

            var item2 = new MyItem
            {
                MyGuid      = new Guid("11ba7957-5afb-4b59-9d9b-c06a18cda5c3"),
                MyInt       = 2,
                MyDouble    = 2.5,
                MyString    = "my value 2",
                MyBool      = false,
                MyEnumValue = MyEnum.Default,
                MyDate      = new DateTime(1990, 10, 12),
                MyObj       = new MyInnerItem {
                    MyInnerString = "my inner value 2"
                },
                MyList = new List <string> {
                    "a2", "b2"
                },
                MyArray = new string[] { "y2", "z2" }
            };

            var xmlArray = new XmlArray("MyItems")
                           .Add(new XmlObject("MyItem")
                                .AddString("MyGuid", item.MyGuid.ToString())
                                .AddNumber("MyInt", item.MyInt)
                                .AddNumber("MyDouble", item.MyDouble)
                                .AddString("MyString", item.MyString)
                                .AddString("MyEnumValue", item.MyEnumValue.ToString())
                                .AddString("MyDate", item.MyDate.ToString())
                                .AddObject("MyObj", XmlValue.CreateObject("MyObj").AddString("MyInnerString", "my inner value 1"))
                                .AddArray("MyList", XmlValue.CreateArray("MyList").Add(new XmlString("String", item.MyList[0])).Add(new XmlString("String", item.MyList[1])))
                                .AddArray("MyArray", XmlValue.CreateArray("MyArray").Add(new XmlString("String", item.MyArray[0])).Add(new XmlString("String", item.MyArray[1]))))
                           .Add(new XmlObject("MyItem")
                                .AddString("MyGuid", item2.MyGuid.ToString())
                                .AddNumber("MyInt", item2.MyInt)
                                .AddNumber("MyDouble", item2.MyDouble)
                                .AddString("MyString", item2.MyString)
                                .AddString("MyEnumValue", item2.MyEnumValue.ToString())
                                .AddString("MyDate", item2.MyDate.ToString())
                                .AddObject("MyObj", XmlValue.CreateObject("MyObj").AddString("MyInnerString", "my inner value 2"))
                                .AddArray("MyList", XmlValue.CreateArray("MyList").Add(new XmlString("String", item2.MyList[0])).Add(new XmlString("String", item2.MyList[0])))
                                .AddArray("MyArray", XmlValue.CreateArray("MyArray").Add(new XmlString("String", item2.MyArray[0])).Add(new XmlString("String", item2.MyArray[1]))));

            var result = service.GetRoot(xmlArray);

            Assert.AreEqual("<MyItems><MyItem><MyGuid>11ba7957-5afb-4b59-9d9b-c06a18cda5c2</MyGuid><MyInt>1</MyInt><MyDouble>1.5</MyDouble><MyString>my value 1</MyString><MyEnumValue>Other</MyEnumValue><MyDate>12/12/1990 00:00:00</MyDate><MyObj><MyInnerString>my inner value 1</MyInnerString></MyObj><MyList><String>a1</String><String>b1</String></MyList><MyArray><String>y1</String><String>z1</String></MyArray></MyItem><MyItem><MyGuid>11ba7957-5afb-4b59-9d9b-c06a18cda5c3</MyGuid><MyInt>2</MyInt><MyDouble>2.5</MyDouble><MyString>my value 2</MyString><MyEnumValue>Default</MyEnumValue><MyDate>12/10/1990 00:00:00</MyDate><MyObj><MyInnerString>my inner value 2</MyInnerString></MyObj><MyList><String>a2</String><String>a2</String></MyList><MyArray><String>y2</String><String>z2</String></MyArray></MyItem></MyItems>", result);
        }