PageRenderTime 54ms CodeModel.GetById 2ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/API/MetaWeblog/XMLRPCResponse.cs

#
C# | 1005 lines | 656 code | 132 blank | 217 comment | 8 complexity | c4b98cdf1b750d0b338511b18f0ca894 MD5 | raw file
   1namespace BlogEngine.Core.API.MetaWeblog
   2{
   3    using System;
   4    using System.Collections.Generic;
   5    using System.Text;
   6    using System.Web;
   7    using System.Xml;
   8
   9    /// <summary>
  10    /// Object is the outgoing XML-RPC response.  This objects properties are set
  11    ///     and the Response method is called sending the response via the HttpContext Response.
  12    /// </summary>
  13    internal class XMLRPCResponse
  14    {
  15        #region Constants and Fields
  16
  17        /// <summary>
  18        /// The method name.
  19        /// </summary>
  20        private readonly string methodName;
  21
  22        #endregion
  23
  24        #region Constructors and Destructors
  25
  26        /// <summary>
  27        /// Initializes a new instance of the <see cref="XMLRPCResponse"/> class. 
  28        /// Constructor sets default value
  29        /// </summary>
  30        /// <param name="methodName">
  31        /// MethodName of called XML-RPC method
  32        /// </param>
  33        public XMLRPCResponse(string methodName)
  34        {
  35            this.methodName = methodName;
  36            this.Blogs = new List<MWABlogInfo>();
  37            this.Categories = new List<MWACategory>();
  38            this.Keywords = new List<string>();
  39            this.Posts = new List<MWAPost>();
  40            this.Pages = new List<MWAPage>();
  41            this.Authors = new List<MWAAuthor>();
  42        }
  43
  44        #endregion
  45
  46        #region Properties
  47
  48        /// <summary>
  49        ///     Gets or sets List if author structs.  Used by wp.getAuthors.
  50        /// </summary>
  51        public List<MWAAuthor> Authors { get; set; }
  52
  53        /// <summary>
  54        ///     Gets or sets List of blog structs.  Used by blogger.getUsersBlogs.
  55        /// </summary>
  56        public List<MWABlogInfo> Blogs { get; set; }
  57
  58        /// <summary>
  59        ///     Gets or sets List of category structs. Used by metaWeblog.getCategories.
  60        /// </summary>
  61        public List<MWACategory> Categories { get; set; }
  62
  63        /// <summary>
  64        ///     Gets or sets a value indicating whether function call was completed and successful.  
  65        ///     Used by metaWeblog.editPost and blogger.deletePost.
  66        /// </summary>
  67        public bool Completed { get; set; }
  68
  69        /// <summary>
  70        ///     Gets or sets Fault Struct. Used by API to return error information
  71        /// </summary>
  72        public MWAFault Fault { get; set; }
  73
  74        /// <summary>
  75        ///     Gets or sets List of Tags.  Used by wp.getTags.
  76        /// </summary>
  77        public List<string> Keywords { get; set; }
  78
  79        /// <summary>
  80        ///     Gets or sets MediaInfo Struct
  81        /// </summary>
  82        public MWAMediaInfo MediaInfo { get; set; }
  83
  84        /// <summary>
  85        ///     Gets or sets MWAPage struct
  86        /// </summary>
  87        public MWAPage Page { get; set; }
  88
  89        /// <summary>
  90        ///     Gets or sets Id of page that was just added.
  91        /// </summary>
  92        public string PageID { get; set; }
  93
  94        /// <summary>
  95        ///     Gets or sets List of Page Structs
  96        /// </summary>
  97        public List<MWAPage> Pages { get; set; }
  98
  99        /// <summary>
 100        ///     Gets or sets Metaweblog Post Struct. Used by metaWeblog.getPost
 101        /// </summary>
 102        public MWAPost Post { get; set; }
 103
 104        /// <summary>
 105        ///     Gets or sets Id of post that was just added.  Used by metaWeblog.newPost
 106        /// </summary>
 107        public string PostID { get; set; }
 108
 109        /// <summary>
 110        ///     Gets or sets List of Metaweblog Post Structs.  Used by metaWeblog.getRecentPosts
 111        /// </summary>
 112        public List<MWAPost> Posts { get; set; }
 113
 114        #endregion
 115
 116        #region Public Methods
 117
 118        /// <summary>
 119        /// Response generates the XML-RPC response and returns it to the caller.
 120        /// </summary>
 121        /// <param name="context">
 122        /// httpContext.Response.OutputStream is used from the context
 123        /// </param>
 124        public void Response(HttpContext context)
 125        {
 126            context.Response.ContentType = "text/xml";
 127            using (var data = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
 128            {
 129                data.Formatting = Formatting.Indented;
 130                data.WriteStartDocument();
 131                data.WriteStartElement("methodResponse");
 132                data.WriteStartElement(this.methodName == "fault" ? "fault" : "params");
 133
 134                switch (this.methodName)
 135                {
 136                    case "metaWeblog.newPost":
 137                        this.WriteNewPost(data);
 138                        break;
 139                    case "metaWeblog.getPost":
 140                        this.WritePost(data);
 141                        break;
 142                    case "metaWeblog.newMediaObject":
 143                        this.WriteMediaInfo(data);
 144                        break;
 145                    case "metaWeblog.getCategories":
 146                        this.WriteGetCategories(data);
 147                        break;
 148                    case "metaWeblog.getRecentPosts":
 149                        this.WritePosts(data);
 150                        break;
 151                    case "blogger.getUsersBlogs":
 152                    case "metaWeblog.getUsersBlogs":
 153                        this.WriteGetUsersBlogs(data);
 154                        break;
 155                    case "metaWeblog.editPost":
 156                    case "blogger.deletePost":
 157                    case "wp.editPage":
 158                    case "wp.deletePage":
 159                        this.WriteBool(data);
 160                        break;
 161                    case "wp.newPage":
 162                        this.WriteNewPage(data);
 163                        break;
 164                    case "wp.getPage":
 165                        this.WritePage(data);
 166                        break;
 167                    case "wp.getPageList":
 168                        this.WriteShortPages(data);
 169                        break;
 170                    case "wp.getPages":
 171                        this.WritePages(data);
 172                        break;
 173                    case "wp.getAuthors":
 174                        this.WriteAuthors(data);
 175                        break;
 176                    case "wp.getTags":
 177                        this.WriteKeywords(data);
 178                        break;
 179                    case "fault":
 180                        this.WriteFault(data);
 181                        break;
 182                }
 183
 184                data.WriteEndElement();
 185                data.WriteEndElement();
 186                data.WriteEndDocument();
 187            }
 188        }
 189
 190        #endregion
 191
 192        #region Methods
 193
 194        /// <summary>
 195        /// Convert Date to format expected by MetaWeblog Response.
 196        /// </summary>
 197        /// <param name="date">
 198        /// DateTime to convert
 199        /// </param>
 200        /// <returns>
 201        /// ISO8601 date string
 202        /// </returns>
 203        private static string ConvertDatetoISO8601(DateTime date)
 204        {
 205            var temp = string.Format(
 206                "{0}{1}{2}T{3}:{4}:{5}",
 207                date.Year,
 208                date.Month.ToString().PadLeft(2, '0'),
 209                date.Day.ToString().PadLeft(2, '0'),
 210                date.Hour.ToString().PadLeft(2, '0'),
 211                date.Minute.ToString().PadLeft(2, '0'),
 212                date.Second.ToString().PadLeft(2, '0'));
 213            return temp;
 214        }
 215
 216        /// <summary>
 217        /// Writes the Array of Category structs parameters of Response
 218        /// </summary>
 219        /// <param name="data">
 220        /// xml response
 221        /// </param>
 222        private void WriteAuthors(XmlWriter data)
 223        {
 224            data.WriteStartElement("param");
 225            data.WriteStartElement("value");
 226            data.WriteStartElement("array");
 227            data.WriteStartElement("data");
 228
 229            foreach (var author in this.Authors)
 230            {
 231                data.WriteStartElement("value");
 232                data.WriteStartElement("struct");
 233
 234                // user id
 235                data.WriteStartElement("member");
 236                data.WriteElementString("name", "user_id");
 237                data.WriteElementString("value", author.user_id);
 238                data.WriteEndElement();
 239
 240                // login
 241                data.WriteStartElement("member");
 242                data.WriteElementString("name", "user_login");
 243                data.WriteElementString("value", author.user_login);
 244                data.WriteEndElement();
 245
 246                // display name 
 247                data.WriteStartElement("member");
 248                data.WriteElementString("name", "display_name");
 249                data.WriteElementString("value", author.display_name);
 250                data.WriteEndElement();
 251
 252                // user email
 253                data.WriteStartElement("member");
 254                data.WriteElementString("name", "user_email");
 255                data.WriteElementString("value", author.user_email);
 256                data.WriteEndElement();
 257
 258                // meta value
 259                data.WriteStartElement("member");
 260                data.WriteElementString("name", "meta_value");
 261                data.WriteElementString("value", author.meta_value);
 262                data.WriteEndElement();
 263
 264                data.WriteEndElement();
 265                data.WriteEndElement();
 266            }
 267
 268            // Close tags
 269            data.WriteEndElement();
 270            data.WriteEndElement();
 271            data.WriteEndElement();
 272            data.WriteEndElement();
 273        }
 274
 275        /// <summary>
 276        /// Writes Boolean parameter of Response
 277        /// </summary>
 278        /// <param name="data">
 279        /// xml response
 280        /// </param>
 281        private void WriteBool(XmlWriter data)
 282        {
 283            var postValue = "0";
 284            if (this.Completed)
 285            {
 286                postValue = "1";
 287            }
 288
 289            data.WriteStartElement("param");
 290            data.WriteStartElement("value");
 291            data.WriteElementString("boolean", postValue);
 292            data.WriteEndElement();
 293            data.WriteEndElement();
 294        }
 295
 296        /// <summary>
 297        /// Writes Fault Parameters of Response.
 298        /// </summary>
 299        /// <param name="data">
 300        /// xml response
 301        /// </param>
 302        private void WriteFault(XmlWriter data)
 303        {
 304            data.WriteStartElement("value");
 305            data.WriteStartElement("struct");
 306
 307            // faultCode
 308            data.WriteStartElement("member");
 309            data.WriteElementString("name", "faultCode");
 310            data.WriteElementString("value", this.Fault.faultCode);
 311            data.WriteEndElement();
 312
 313            // faultString
 314            data.WriteStartElement("member");
 315            data.WriteElementString("name", "faultString");
 316            data.WriteElementString("value", this.Fault.faultString);
 317            data.WriteEndElement();
 318
 319            data.WriteEndElement();
 320            data.WriteEndElement();
 321        }
 322
 323        /// <summary>
 324        /// Writes the Array of Category structs parameters of Response
 325        /// </summary>
 326        /// <param name="data">
 327        /// xml response
 328        /// </param>
 329        private void WriteGetCategories(XmlWriter data)
 330        {
 331            data.WriteStartElement("param");
 332            data.WriteStartElement("value");
 333            data.WriteStartElement("array");
 334            data.WriteStartElement("data");
 335
 336            foreach (var category in this.Categories)
 337            {
 338                data.WriteStartElement("value");
 339                data.WriteStartElement("struct");
 340
 341                // description
 342                data.WriteStartElement("member");
 343                data.WriteElementString("name", "description");
 344                data.WriteElementString("value", category.description);
 345                data.WriteEndElement();
 346
 347                // categoryid
 348                data.WriteStartElement("member");
 349                data.WriteElementString("name", "categoryid");
 350                data.WriteElementString("value", category.id);
 351                data.WriteEndElement();
 352
 353                // title
 354                data.WriteStartElement("member");
 355                data.WriteElementString("name", "title");
 356                data.WriteElementString("value", category.title);
 357                data.WriteEndElement();
 358
 359                // htmlUrl 
 360                data.WriteStartElement("member");
 361                data.WriteElementString("name", "htmlUrl");
 362                data.WriteElementString("value", category.htmlUrl);
 363                data.WriteEndElement();
 364
 365                // rssUrl
 366                data.WriteStartElement("member");
 367                data.WriteElementString("name", "rssUrl");
 368                data.WriteElementString("value", category.rssUrl);
 369                data.WriteEndElement();
 370
 371                data.WriteEndElement();
 372                data.WriteEndElement();
 373            }
 374
 375            // Close tags
 376            data.WriteEndElement();
 377            data.WriteEndElement();
 378            data.WriteEndElement();
 379            data.WriteEndElement();
 380        }
 381
 382        /// <summary>
 383        /// Writes array of BlogInfo structs of Response
 384        /// </summary>
 385        /// <param name="data">
 386        /// xml response
 387        /// </param>
 388        private void WriteGetUsersBlogs(XmlWriter data)
 389        {
 390            data.WriteStartElement("param");
 391            data.WriteStartElement("value");
 392            data.WriteStartElement("array");
 393            data.WriteStartElement("data");
 394
 395            foreach (var blog in this.Blogs)
 396            {
 397                data.WriteStartElement("value");
 398                data.WriteStartElement("struct");
 399
 400                // url
 401                data.WriteStartElement("member");
 402                data.WriteElementString("name", "url");
 403                data.WriteElementString("value", blog.url);
 404                data.WriteEndElement();
 405
 406                // blogid
 407                data.WriteStartElement("member");
 408                data.WriteElementString("name", "blogid");
 409                data.WriteElementString("value", blog.blogID);
 410                data.WriteEndElement();
 411
 412                // blogName
 413                data.WriteStartElement("member");
 414                data.WriteElementString("name", "blogName");
 415                data.WriteElementString("value", blog.blogName);
 416                data.WriteEndElement();
 417
 418                data.WriteEndElement();
 419                data.WriteEndElement();
 420            }
 421
 422            // Close tags
 423            data.WriteEndElement();
 424            data.WriteEndElement();
 425            data.WriteEndElement();
 426            data.WriteEndElement();
 427        }
 428
 429        /// <summary>
 430        /// Writes the Array of Keyword structs parameters of Response
 431        /// </summary>
 432        /// <param name="data">
 433        /// xml response
 434        /// </param>
 435        private void WriteKeywords(XmlWriter data)
 436        {
 437            data.WriteStartElement("param");
 438            data.WriteStartElement("value");
 439            data.WriteStartElement("array");
 440            data.WriteStartElement("data");
 441
 442            foreach (var keyword in this.Keywords)
 443            {
 444                data.WriteStartElement("value");
 445                data.WriteStartElement("struct");
 446
 447                // keywordName
 448                data.WriteStartElement("member");
 449                data.WriteElementString("name", "name");
 450                data.WriteElementString("value", keyword);
 451                data.WriteEndElement();
 452
 453                data.WriteEndElement();
 454                data.WriteEndElement();
 455            }
 456
 457            // Close tags
 458            data.WriteEndElement();
 459            data.WriteEndElement();
 460            data.WriteEndElement();
 461            data.WriteEndElement();
 462        }
 463
 464        /// <summary>
 465        /// Writes the MediaInfo Struct of Response
 466        /// </summary>
 467        /// <param name="data">
 468        /// xml response
 469        /// </param>
 470        private void WriteMediaInfo(XmlWriter data)
 471        {
 472            data.WriteStartElement("param");
 473            data.WriteStartElement("value");
 474            data.WriteStartElement("struct");
 475
 476            // url
 477            data.WriteStartElement("member");
 478            data.WriteElementString("name", "url");
 479            data.WriteStartElement("value");
 480            data.WriteElementString("string", this.MediaInfo.url);
 481            data.WriteEndElement();
 482            data.WriteEndElement();
 483
 484            data.WriteEndElement();
 485            data.WriteEndElement();
 486            data.WriteEndElement();
 487        }
 488
 489        /// <summary>
 490        /// Writes the PageID string of Response.
 491        /// </summary>
 492        /// <param name="data">
 493        /// xml response
 494        /// </param>
 495        private void WriteNewPage(XmlWriter data)
 496        {
 497            data.WriteStartElement("param");
 498            data.WriteStartElement("value");
 499            data.WriteElementString("string", this.PageID);
 500            data.WriteEndElement();
 501            data.WriteEndElement();
 502        }
 503
 504        /// <summary>
 505        /// Writes the PostID string of Response.
 506        /// </summary>
 507        /// <param name="data">
 508        /// xml response
 509        /// </param>
 510        private void WriteNewPost(XmlWriter data)
 511        {
 512            data.WriteStartElement("param");
 513            data.WriteStartElement("value");
 514            data.WriteElementString("string", this.PostID);
 515            data.WriteEndElement();
 516            data.WriteEndElement();
 517        }
 518
 519        /// <summary>
 520        /// Writes the Metaweblog Post Struct of Response.
 521        /// </summary>
 522        /// <param name="data">
 523        /// xml response
 524        /// </param>
 525        private void WritePage(XmlWriter data)
 526        {
 527            data.WriteStartElement("param");
 528            data.WriteStartElement("value");
 529            data.WriteStartElement("struct");
 530
 531            // pageid
 532            data.WriteStartElement("member");
 533            data.WriteElementString("name", "page_id");
 534            data.WriteStartElement("value");
 535            data.WriteElementString("string", this.Page.pageID);
 536            data.WriteEndElement();
 537            data.WriteEndElement();
 538
 539            // title
 540            data.WriteStartElement("member");
 541            data.WriteElementString("name", "title");
 542            data.WriteStartElement("value");
 543            data.WriteElementString("string", this.Page.title);
 544            data.WriteEndElement();
 545            data.WriteEndElement();
 546
 547            // description
 548            data.WriteStartElement("member");
 549            data.WriteElementString("name", "description");
 550            data.WriteStartElement("value");
 551            data.WriteElementString("string", this.Page.description);
 552            data.WriteEndElement();
 553            data.WriteEndElement();
 554
 555            // link
 556            data.WriteStartElement("member");
 557            data.WriteElementString("name", "link");
 558            data.WriteStartElement("value");
 559            data.WriteElementString("string", this.Page.link);
 560            data.WriteEndElement();
 561            data.WriteEndElement();
 562
 563            // mt_convert_breaks
 564            data.WriteStartElement("member");
 565            data.WriteElementString("name", "mt_convert_breaks");
 566            data.WriteStartElement("value");
 567            data.WriteElementString("string", "__default__");
 568            data.WriteEndElement();
 569            data.WriteEndElement();
 570
 571            // dateCreated
 572            data.WriteStartElement("member");
 573            data.WriteElementString("name", "dateCreated");
 574            data.WriteStartElement("value");
 575            data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(this.Page.pageDate));
 576            data.WriteEndElement();
 577            data.WriteEndElement();
 578
 579            data.WriteEndElement();
 580            data.WriteEndElement();
 581            data.WriteEndElement();
 582        }
 583
 584        /// <summary>
 585        /// The write pages.
 586        /// </summary>
 587        /// <param name="data">
 588        /// xml response
 589        /// </param>
 590        private void WritePages(XmlWriter data)
 591        {
 592            data.WriteStartElement("param");
 593            data.WriteStartElement("value");
 594            data.WriteStartElement("array");
 595            data.WriteStartElement("data");
 596
 597            foreach (var page in this.Pages)
 598            {
 599                data.WriteStartElement("value");
 600                data.WriteStartElement("struct");
 601
 602                // pageid
 603                data.WriteStartElement("member");
 604                data.WriteElementString("name", "page_id");
 605                data.WriteStartElement("value");
 606                data.WriteElementString("string", page.pageID);
 607                data.WriteEndElement();
 608                data.WriteEndElement();
 609
 610                // title
 611                data.WriteStartElement("member");
 612                data.WriteElementString("name", "title");
 613                data.WriteStartElement("value");
 614                data.WriteElementString("string", page.title);
 615                data.WriteEndElement();
 616                data.WriteEndElement();
 617
 618                // description
 619                data.WriteStartElement("member");
 620                data.WriteElementString("name", "description");
 621                data.WriteStartElement("value");
 622                data.WriteElementString("string", page.description);
 623                data.WriteEndElement();
 624                data.WriteEndElement();
 625
 626                // link
 627                data.WriteStartElement("member");
 628                data.WriteElementString("name", "link");
 629                data.WriteStartElement("value");
 630                data.WriteElementString("string", page.link);
 631                data.WriteEndElement();
 632                data.WriteEndElement();
 633
 634                // mt_convert_breaks
 635                data.WriteStartElement("member");
 636                data.WriteElementString("name", "mt_convert_breaks");
 637                data.WriteStartElement("value");
 638                data.WriteElementString("string", "__default__");
 639                data.WriteEndElement();
 640                data.WriteEndElement();
 641
 642                // dateCreated
 643                data.WriteStartElement("member");
 644                data.WriteElementString("name", "dateCreated");
 645                data.WriteStartElement("value");
 646                data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(page.pageDate));
 647                data.WriteEndElement();
 648                data.WriteEndElement();
 649
 650                // page_parent_id
 651                if (!string.IsNullOrEmpty(page.pageParentID))
 652                {
 653                    data.WriteStartElement("member");
 654                    data.WriteElementString("name", "page_parent_id");
 655                    data.WriteStartElement("value");
 656                    data.WriteElementString("string", page.pageParentID);
 657                    data.WriteEndElement();
 658                    data.WriteEndElement();
 659                }
 660
 661                data.WriteEndElement();
 662                data.WriteEndElement();
 663            }
 664
 665            // Close tags
 666            data.WriteEndElement();
 667            data.WriteEndElement();
 668            data.WriteEndElement();
 669            data.WriteEndElement();
 670        }
 671
 672        /// <summary>
 673        /// Writes the Metaweblog Post Struct of Response.
 674        /// </summary>
 675        /// <param name="data">
 676        /// xml response
 677        /// </param>
 678        private void WritePost(XmlWriter data)
 679        {
 680            data.WriteStartElement("param");
 681            data.WriteStartElement("value");
 682            data.WriteStartElement("struct");
 683
 684            // postid
 685            data.WriteStartElement("member");
 686            data.WriteElementString("name", "postid");
 687            data.WriteStartElement("value");
 688            data.WriteElementString("string", this.Post.postID);
 689            data.WriteEndElement();
 690            data.WriteEndElement();
 691
 692            // title
 693            data.WriteStartElement("member");
 694            data.WriteElementString("name", "title");
 695            data.WriteStartElement("value");
 696            data.WriteElementString("string", this.Post.title);
 697            data.WriteEndElement();
 698            data.WriteEndElement();
 699
 700            // description
 701            data.WriteStartElement("member");
 702            data.WriteElementString("name", "description");
 703            data.WriteStartElement("value");
 704            data.WriteElementString("string", this.Post.description);
 705            data.WriteEndElement();
 706            data.WriteEndElement();
 707
 708            // link
 709            data.WriteStartElement("member");
 710            data.WriteElementString("name", "link");
 711            data.WriteStartElement("value");
 712            data.WriteElementString("string", this.Post.link);
 713            data.WriteEndElement();
 714            data.WriteEndElement();
 715
 716            // slug
 717            data.WriteStartElement("member");
 718            data.WriteElementString("name", "wp_slug");
 719            data.WriteStartElement("value");
 720            data.WriteElementString("string", this.Post.slug);
 721            data.WriteEndElement();
 722            data.WriteEndElement();
 723
 724            // excerpt
 725            data.WriteStartElement("member");
 726            data.WriteElementString("name", "mt_excerpt");
 727            data.WriteStartElement("value");
 728            data.WriteElementString("string", this.Post.excerpt);
 729            data.WriteEndElement();
 730            data.WriteEndElement();
 731
 732            // comment policy
 733            data.WriteStartElement("member");
 734            data.WriteElementString("name", "mt_allow_comments");
 735            data.WriteStartElement("value");
 736            data.WriteElementString("int", this.Post.commentPolicy);
 737            data.WriteEndElement();
 738            data.WriteEndElement();
 739
 740            // dateCreated
 741            data.WriteStartElement("member");
 742            data.WriteElementString("name", "dateCreated");
 743            data.WriteStartElement("value");
 744            data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(this.Post.postDate));
 745            data.WriteEndElement();
 746            data.WriteEndElement();
 747
 748            // publish
 749            data.WriteStartElement("member");
 750            data.WriteElementString("name", "publish");
 751            data.WriteStartElement("value");
 752            data.WriteElementString("boolean", this.Post.publish ? "1" : "0");
 753
 754            data.WriteEndElement();
 755            data.WriteEndElement();
 756
 757            // tags (mt_keywords)
 758            data.WriteStartElement("member");
 759            data.WriteElementString("name", "mt_keywords");
 760            data.WriteStartElement("value");
 761            var tags = new string[this.Post.tags.Count];
 762            for (var i = 0; i < this.Post.tags.Count; i++)
 763            {
 764                tags[i] = this.Post.tags[i];
 765            }
 766
 767            var tagList = string.Join(",", tags);
 768            data.WriteElementString("string", tagList);
 769            data.WriteEndElement();
 770            data.WriteEndElement();
 771
 772            // categories
 773            if (this.Post.categories.Count > 0)
 774            {
 775                data.WriteStartElement("member");
 776                data.WriteElementString("name", "categories");
 777                data.WriteStartElement("value");
 778                data.WriteStartElement("array");
 779                data.WriteStartElement("data");
 780                foreach (var cat in this.Post.categories)
 781                {
 782                    data.WriteStartElement("value");
 783                    data.WriteElementString("string", cat);
 784                    data.WriteEndElement();
 785                }
 786
 787                data.WriteEndElement();
 788                data.WriteEndElement();
 789                data.WriteEndElement();
 790                data.WriteEndElement();
 791            }
 792
 793            data.WriteEndElement();
 794            data.WriteEndElement();
 795            data.WriteEndElement();
 796        }
 797
 798        /// <summary>
 799        /// Writes the array of Metaweblog Post Structs of Response.
 800        /// </summary>
 801        /// <param name="data">
 802        /// xml response
 803        /// </param>
 804        private void WritePosts(XmlWriter data)
 805        {
 806            data.WriteStartElement("param");
 807            data.WriteStartElement("value");
 808            data.WriteStartElement("array");
 809            data.WriteStartElement("data");
 810
 811            foreach (var post in this.Posts)
 812            {
 813                data.WriteStartElement("value");
 814                data.WriteStartElement("struct");
 815
 816                // postid
 817                data.WriteStartElement("member");
 818                data.WriteElementString("name", "postid");
 819                data.WriteStartElement("value");
 820                data.WriteElementString("string", post.postID);
 821                data.WriteEndElement();
 822                data.WriteEndElement();
 823
 824                // dateCreated
 825                data.WriteStartElement("member");
 826                data.WriteElementString("name", "dateCreated");
 827                data.WriteStartElement("value");
 828                data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(post.postDate));
 829                data.WriteEndElement();
 830                data.WriteEndElement();
 831
 832                // title
 833                data.WriteStartElement("member");
 834                data.WriteElementString("name", "title");
 835                data.WriteStartElement("value");
 836                data.WriteElementString("string", post.title);
 837                data.WriteEndElement();
 838                data.WriteEndElement();
 839
 840                // description
 841                data.WriteStartElement("member");
 842                data.WriteElementString("name", "description");
 843                data.WriteElementString("value", post.description);
 844                data.WriteEndElement();
 845
 846                // link
 847                data.WriteStartElement("member");
 848                data.WriteElementString("name", "link");
 849                data.WriteStartElement("value");
 850                data.WriteElementString("string", post.link);
 851                data.WriteEndElement();
 852                data.WriteEndElement();
 853
 854                // slug
 855                data.WriteStartElement("member");
 856                data.WriteElementString("name", "wp_slug");
 857                data.WriteStartElement("value");
 858                data.WriteElementString("string", post.slug);
 859                data.WriteEndElement();
 860                data.WriteEndElement();
 861
 862                // excerpt
 863                data.WriteStartElement("member");
 864                data.WriteElementString("name", "mt_excerpt");
 865                data.WriteStartElement("value");
 866                data.WriteElementString("string", post.excerpt);
 867                data.WriteEndElement();
 868                data.WriteEndElement();
 869
 870                // comment policy
 871                data.WriteStartElement("member");
 872                data.WriteElementString("name", "mt_allow_comments");
 873                data.WriteStartElement("value");
 874                data.WriteElementString("string", post.commentPolicy);
 875                data.WriteEndElement();
 876                data.WriteEndElement();
 877
 878                // tags (mt_keywords)
 879                data.WriteStartElement("member");
 880                data.WriteElementString("name", "mt_keywords");
 881                data.WriteStartElement("value");
 882                var tags = new string[post.tags.Count];
 883                for (var i = 0; i < post.tags.Count; i++)
 884                {
 885                    tags[i] = post.tags[i];
 886                }
 887
 888                var tagList = string.Join(",", tags);
 889                data.WriteElementString("string", tagList);
 890                data.WriteEndElement();
 891                data.WriteEndElement();
 892
 893                // publish
 894                data.WriteStartElement("member");
 895                data.WriteElementString("name", "publish");
 896                data.WriteStartElement("value");
 897                data.WriteElementString("boolean", post.publish ? "1" : "0");
 898
 899                data.WriteEndElement();
 900                data.WriteEndElement();
 901
 902                // categories
 903                if (post.categories.Count > 0)
 904                {
 905                    data.WriteStartElement("member");
 906                    data.WriteElementString("name", "categories");
 907                    data.WriteStartElement("value");
 908                    data.WriteStartElement("array");
 909                    data.WriteStartElement("data");
 910                    foreach (var cat in post.categories)
 911                    {
 912                        data.WriteStartElement("value");
 913                        data.WriteElementString("string", cat);
 914                        data.WriteEndElement();
 915                    }
 916
 917                    data.WriteEndElement();
 918                    data.WriteEndElement();
 919                    data.WriteEndElement();
 920                    data.WriteEndElement();
 921                }
 922
 923                data.WriteEndElement();
 924                data.WriteEndElement();
 925            }
 926
 927            // Close tags
 928            data.WriteEndElement();
 929            data.WriteEndElement();
 930            data.WriteEndElement();
 931            data.WriteEndElement();
 932        }
 933
 934        /// <summary>
 935        /// The write short pages.
 936        /// </summary>
 937        /// <param name="data">
 938        /// xml response
 939        /// </param>
 940        private void WriteShortPages(XmlWriter data)
 941        {
 942            data.WriteStartElement("param");
 943            data.WriteStartElement("value");
 944            data.WriteStartElement("array");
 945            data.WriteStartElement("data");
 946
 947            foreach (var page in this.Pages)
 948            {
 949                data.WriteStartElement("value");
 950                data.WriteStartElement("struct");
 951
 952                // pageid
 953                data.WriteStartElement("member");
 954                data.WriteElementString("name", "page_id");
 955                data.WriteStartElement("value");
 956                data.WriteElementString("string", page.pageID);
 957                data.WriteEndElement();
 958                data.WriteEndElement();
 959
 960                // title
 961                data.WriteStartElement("member");
 962                data.WriteElementString("name", "page_title");
 963                data.WriteStartElement("value");
 964                data.WriteElementString("string", page.title);
 965                data.WriteEndElement();
 966                data.WriteEndElement();
 967
 968                // page_parent_id
 969                data.WriteStartElement("member");
 970                data.WriteElementString("name", "page_parent_id");
 971                data.WriteStartElement("value");
 972                data.WriteElementString("string", page.pageParentID);
 973                data.WriteEndElement();
 974                data.WriteEndElement();
 975
 976                // dateCreated
 977                data.WriteStartElement("member");
 978                data.WriteElementString("name", "dateCreated");
 979                data.WriteStartElement("value");
 980                data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(page.pageDate));
 981                data.WriteEndElement();
 982                data.WriteEndElement();
 983
 984                // dateCreated gmt
 985                data.WriteStartElement("member");
 986                data.WriteElementString("name", "date_created_gmt");
 987                data.WriteStartElement("value");
 988                data.WriteElementString("dateTime.iso8601", ConvertDatetoISO8601(page.pageDate));
 989                data.WriteEndElement();
 990                data.WriteEndElement();
 991
 992                data.WriteEndElement();
 993                data.WriteEndElement();
 994            }
 995
 996            // Close tags
 997            data.WriteEndElement();
 998            data.WriteEndElement();
 999            data.WriteEndElement();
1000            data.WriteEndElement();
1001        }
1002
1003        #endregion
1004    }
1005}