Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcReader"/> class.
        /// </summary>
        public XmlRpcReader()
        {
            _dispatch = new Dictionary <string, Func <XElement, XRpcData> >
            {
                { "i4", x => new XRpcData <int> {
                      Value = (int)x
                  } },
                { "int", x => new XRpcData <int> {
                      Value = (int)x
                  } },
                { "boolean", x => new XRpcData <bool> {
                      Value = (string)x == "1"
                  } },
                { "string", x => new XRpcData <string> {
                      Value = (string)x
                  } },
                { "double", x => new XRpcData <double> {
                      Value = (double)x
                  } },
                { "dateTime.iso8601", x => {
                      DateTime parsedDateTime;

                      // try parsing a "normal" datetime string then try what live writer gives us
                      if (!DateTime.TryParse(x.Value, out parsedDateTime) &&
                          !DateTime.TryParseExact(x.Value, "yyyyMMddTHH:mm:ss", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out parsedDateTime))
                      {
                          parsedDateTime = DateTime.Now;
                      }

                      return(new XRpcData <DateTime> {
                            Value = parsedDateTime
                        });
                  } },
                { "base64", x => new XRpcData <byte[]> {
                      Value = Convert.FromBase64String((string)x)
                  } },
                { "struct", x => XRpcData.For(MapToStruct(x)) },
                { "array", x => XRpcData.For(MapToArray(x)) },
            };
        }
        public void ArrayAndStructShouldWorkAsExpected()
        {
            var mapper = new XmlRpcWriter();

            var arr         = new XRpcArray();
            var structParam = XRpcData.For(new XRpcStruct());

            arr.Data.Add(structParam);
            arr.Data.Add(XRpcData.For(19));

            structParam.Value.Members.Add("Hello", XRpcData.For("world"));

            var element = mapper.MapArray(arr);

            Assert.That(NoSpace(element.ToString()), Is.EqualTo(NoSpace(@"
<array><data>
<value><struct>
<member><name>Hello</name><value><string>world</string></value></member>
</struct></value>
<value><int>19</int></value>
</data></array>
")));
        }