//// /// Copyright (c) 2016 Saúl Piña . /// /// This file is part of xmlrpcwsc. /// /// xmlrpcwsc is free software: you can redistribute it and/or modify /// it under the terms of the GNU Lesser General Public License as published by /// the Free Software Foundation, either version 3 of the License, or /// (at your option) any later version. /// /// xmlrpcwsc is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU Lesser General Public License for more details. /// /// You should have received a copy of the GNU Lesser General Public License /// along with xmlrpcwsc. If not, see . //// using System; using System.Xml; using System.Collections.Generic; namespace XmlRpc { /// /// XML-RPC request factory /// public class RequestFactory { /// /// Builds the xml request /// /// The xml request /// Request public static XmlDocument BuildRequest(XmlRpcRequest request) { XmlDocument doc = new XmlDocument(); XmlElement xmlMethodCall = doc.CreateElement("methodCall"); doc.AppendChild(xmlMethodCall); XmlElement xmlMethodName = doc.CreateElement("methodName"); xmlMethodName.InnerText = request.MethodName ?? ""; xmlMethodCall.AppendChild(xmlMethodName); XmlElement xmlParams = doc.CreateElement("params"); xmlMethodCall.AppendChild(xmlParams); if (request.GetParamsCount() <= 0) return doc; List parameters = request.GetParams(); foreach (object parameter in parameters) { XmlElement xmlParam = doc.CreateElement("param"); xmlParams.AppendChild(xmlParam); xmlParam.AppendChild(doc.ImportNode(BuildXmlValue(parameter), true)); } return doc; } /// /// Builds the xml value /// /// The xml value /// Value private static XmlElement BuildXmlValue(object value) { XmlDocument doc = new XmlDocument(); XmlElement xmlValue = doc.CreateElement("value"); if (value is bool) { XmlElement xmlType = doc.CreateElement("boolean"); xmlType.InnerText = (bool)value ? "1" : "0"; xmlValue.AppendChild(xmlType); } else if (value is int) { XmlElement xmlType = doc.CreateElement("int"); xmlType.InnerText = value.ToString(); xmlValue.AppendChild(xmlType); } else if (value is double) { XmlElement xmlType = doc.CreateElement("double"); xmlType.InnerText = value.ToString(); xmlValue.AppendChild(xmlType); } else if (value is DateTime) { XmlElement xmlType = doc.CreateElement("dateTime.iso8601"); // EXAMPLE 19980717T14:08:55 xmlType.InnerText = string.Format("{0:yyyyMMdd'T'HH:mm:ss}", value); xmlValue.AppendChild(xmlType); } else if (value is byte[]) { XmlElement xmlType = doc.CreateElement("base64"); xmlType.InnerText = Convert.ToBase64String((byte[])value); xmlValue.AppendChild(xmlType); } else if (value is Dictionary) { XmlElement xmlType = doc.CreateElement("struct"); xmlValue.AppendChild(xmlType); foreach (KeyValuePair item in (Dictionary) value) { XmlElement xmlMember = doc.CreateElement("member"); xmlType.AppendChild(xmlMember); XmlElement xmlName = doc.CreateElement("name"); xmlName.InnerText = item.Key; xmlMember.AppendChild(xmlName); xmlMember.AppendChild(doc.ImportNode(BuildXmlValue(item.Value), true)); } } else if (value is List) { XmlElement xmlType = doc.CreateElement("array"); xmlValue.AppendChild(xmlType); XmlElement xmlData = doc.CreateElement("data"); xmlType.AppendChild(xmlData); foreach (object item in (List) value) { xmlData.AppendChild(doc.ImportNode(BuildXmlValue(item), true)); } } else if (value is object[]) { XmlElement xmlType = doc.CreateElement("array"); xmlValue.AppendChild(xmlType); XmlElement xmlData = doc.CreateElement("data"); xmlType.AppendChild(xmlData); foreach (object item in (object[]) value) { xmlData.AppendChild(doc.ImportNode(BuildXmlValue(item), true)); } } else { XmlElement xmlType = doc.CreateElement("string"); xmlType.InnerText = value.ToString(); xmlValue.AppendChild(xmlType); } return xmlValue; } } }