예제 #1
0
        private void ParseElement(XElement element)
        {
            if (!element.HasElements)
            {
                throw new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE);
            }

            string _mtTextMore = null;

            foreach (XElement member in element.Descendants(XmlRPCResponseConstants.MEMBER))
            {
                string value      = string.Empty;
                string memberName = member.Element(XmlRPCResponseConstants.NAME).Value;
                if (DATECREATED_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.DATETIMEISO8601).First().Value;
                    DateTime tempDate;
                    if (DateTime.TryParseExact(value, Constants.WORDPRESS_DATEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate))
                    {
                        _dateCreated = tempDate; //This value will be replaced with the correct value obtained from the GMT -> local time conversion (later in this method).
                    }
                    else
                    {
                        Exception detailedError = new FormatException("Unable to parse dataCreated: " + value);
                        throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, detailedError);
                    }
                }
                else if (USERID_VALUE.Equals(memberName))
                {
                    try
                    {
                        value = member.GetValueAsString(false);
                    }
                    catch (Exception)
                    {
                        _userId = "-1";
                    }
                }
                else if (POSTID_VALUE.Equals(memberName))
                {
                    _postId = member.GetValueAsString(true);
                }
                else if (DESCRIPTION_VALUE.Equals(memberName))
                {
                    value        = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _description = value.HtmlDecode();
                }
                else if (TITLE_VALUE.Equals(memberName))
                {
                    value  = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _title = value.HtmlDecode();
                }
                else if (LINK_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _link = value;
                }
                else if (PERMALINK_VALUE.Equals(memberName))
                {
                    value      = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _permaLink = value;
                }
                else if (CATEGORIES_VALUE.Equals(memberName))
                {
                    foreach (XElement dataElement in member.Descendants(XmlRPCResponseConstants.STRING))
                    {
                        _categories.Add(dataElement.Value.HtmlDecode());
                    }
                }
                else if (MTEXCERPT_VALUE.Equals(memberName))
                {
                    value      = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtExcerpt = value.HtmlDecode();
                }
                else if (MTTEXTMORE_VALUE.Equals(memberName))
                {
                    value       = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtTextMore = value.HtmlDecode();
                }
                else if (MTALLOWCOMMENTS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.INT).First().Value;
                    try
                    {
                        _mtAllowComments = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _mtAllowComments = false;
                    }
                }
                else if (MTALLOWPINGS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.INT).First().Value;
                    try
                    {
                        _mtAllowPings = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _mtAllowPings = false;
                    }
                }
                else if (MTKEYWORDS_VALUE.Equals(memberName))
                {
                    value       = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtKeyWords = value.HtmlDecode();
                }
                else if (WPSLUG_VALUE.Equals(memberName))
                {
                    value   = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpSlug = value.HtmlDecode();
                }
                else if (WPPASSWORD_VALUE.Equals(memberName))
                {
                    value       = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpPassword = value.HtmlDecode();
                }
                else if (WPAUTHORID_VALUE.Equals(memberName))
                {
                    _wpAuthorId = member.GetValueAsString(false);
                }
                else if (WPAUTHORDISPLAYNAME_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpAuthorDisplayName = value.HtmlDecode();
                }
                else if (DATECREATEDGMT_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.DATETIMEISO8601).First().Value;
                    DateTime tempDate;
                    if (DateTime.TryParseExact(value, Constants.WORDPRESS_DATEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate))
                    {
                        _dateCreatedGMT = tempDate;
                        _dateCreated    = tempDate.ToLocalTime(); //set the post's local time by using the device timezone offset.
                    }
                    else
                    {
                        Exception detailedError = new FormatException("Unable to parse GMT date-time: " + value);
                        throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, detailedError);
                    }
                }
                else if (POSTSTATUS_VALUE.Equals(memberName))
                {
                    value       = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postStatus = value;
                }
                else if (CUSTOMFIELDS_VALUE.Equals(memberName))
                {
                    CustomField field = null;
                    foreach (XElement childStructElement in element.Descendants(XmlRPCResponseConstants.STRUCT))
                    {
                        field = new CustomField(childStructElement);
                        _customFields.Add(field);
                    }
                }
                else if (STICKY_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.BOOLEAN).First().Value;
                    try
                    {
                        _sticky = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _sticky = false;
                    }
                }
                else if (POSTFORMAT_VALUE.Equals(memberName))
                {
                    value       = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postFormat = value;
                }
                else if (POSTTHUMBNAIL_VALUE.Equals(memberName))
                {
                    value          = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postThumbnail = value;
                }
            } // end for-each

            if (!String.IsNullOrEmpty(_mtTextMore))
            {
                _description = _description + "<!--more-->\n" + _mtTextMore;
            }

            IsNew = false;
        }
예제 #2
0
        private void ParseElement(XElement element)
        {
            if (!element.HasElements)
            {
                throw new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE);
            }

            string _mtTextMore = null;
            foreach (XElement member in element.Descendants(XmlRPCResponseConstants.MEMBER))
            {
                string value = string.Empty;
                string memberName = member.Element(XmlRPCResponseConstants.NAME).Value;
                if (DATECREATED_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.DATETIMEISO8601).First().Value;
                    DateTime tempDate;
                    if (DateTime.TryParseExact(value, Constants.WORDPRESS_DATEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate))
                    {
                        _dateCreated = tempDate; //This value will be replaced with the correct value obtained from the GMT -> local time conversion (later in this method).
                    }
                    else
                    {
                        Exception detailedError = new FormatException("Unable to parse dataCreated: " + value);
                        throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, detailedError);
                    }
                }
                else if (USERID_VALUE.Equals(memberName))
                {
                    try
                    {
                        value = member.GetValueAsString(false);
                    }
                    catch (Exception)
                    {
                        _userId = "-1";
                    }
                }
                else if (POSTID_VALUE.Equals(memberName))
                {
                    _postId = member.GetValueAsString(true);
                }
                else if (DESCRIPTION_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _description = value.HtmlDecode();
                }
                else if (TITLE_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _title = value.HtmlDecode();
                }
                else if (LINK_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _link = value;
                }
                else if (PERMALINK_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _permaLink = value;
                }
                else if (CATEGORIES_VALUE.Equals(memberName))
                {
                    foreach (XElement dataElement in member.Descendants(XmlRPCResponseConstants.STRING))
                    {
                        _categories.Add(dataElement.Value.HtmlDecode());
                    }
                }
                else if (MTEXCERPT_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtExcerpt = value.HtmlDecode();
                }
                else if (MTTEXTMORE_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtTextMore = value.HtmlDecode();
                }
                else if (MTALLOWCOMMENTS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.INT).First().Value;
                    try
                    {
                        _mtAllowComments = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _mtAllowComments = false;
                    }
                }
                else if (MTALLOWPINGS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.INT).First().Value;
                    try
                    {
                        _mtAllowPings = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _mtAllowPings = false;
                    }
                }
                else if (MTKEYWORDS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _mtKeyWords = value.HtmlDecode();
                }
                else if (WPSLUG_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpSlug = value.HtmlDecode();
                }
                else if (WPPASSWORD_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpPassword = value.HtmlDecode();
                }
                else if (WPAUTHORID_VALUE.Equals(memberName))
                {
                    _wpAuthorId = member.GetValueAsString(false);
                }
                else if (WPAUTHORDISPLAYNAME_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _wpAuthorDisplayName = value.HtmlDecode();
                }
                else if (DATECREATEDGMT_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.DATETIMEISO8601).First().Value;
                    DateTime tempDate;
                    if (DateTime.TryParseExact(value, Constants.WORDPRESS_DATEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate))
                    {
                        _dateCreatedGMT = tempDate;
                        _dateCreated = tempDate.ToLocalTime(); //set the post's local time by using the device timezone offset.
                    }
                    else
                    {
                        Exception detailedError = new FormatException("Unable to parse GMT date-time: " + value);
                        throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, detailedError);
                    }
                }
                else if (POSTSTATUS_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postStatus = value;
                }
                else if (CUSTOMFIELDS_VALUE.Equals(memberName))
                {
                    CustomField field = null;
                    foreach (XElement childStructElement in element.Descendants(XmlRPCResponseConstants.STRUCT))
                    {
                        field = new CustomField(childStructElement);
                        _customFields.Add(field);
                    }
                }
                else if (STICKY_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.BOOLEAN).First().Value;
                    try
                    {
                        _sticky = Convert.ToBoolean(Convert.ToInt16(value));
                    }
                    catch
                    {
                        _sticky = false;
                    }
                }
                else if (POSTFORMAT_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postFormat = value;
                }
                else if (POSTTHUMBNAIL_VALUE.Equals(memberName))
                {
                    value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
                    _postThumbnail = value;
                }
            } // end for-each

            if (!String.IsNullOrEmpty(_mtTextMore))
                _description = _description + "<!--more-->\n" + _mtTextMore;

            IsNew = false;
        }