title: C# Send XmlDocument to Xml-RPC Server API
body: <p>I am answering my own question because I had problems to find a solution for this. Maybe it will help someone. I had a request to send a XML to an Https API that only accepts XML-RPC request.</p>
  
  <p>This article helped me to understand XML-RPC: <a href="http://codinghints.blogspot.co.at/2010/03/xml-rpc-calls-with-c.html" rel="nofollow noreferrer">http://codinghints.blogspot.co.at/2010/03/xml-rpc-calls-with-c.html</a></p>
  
  <p>And this is how I solved it:</p>
  
  <p><strong>Using</strong></p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.IO;
  using System.Net;
  using System.Net.Http;
  using System.Text;
  using System.Xml;
  </code></pre>
  
  <p><strong>1. My method to create a XmlDocument</strong></p>
  
  <p>(Only needed if there is not already a XmlDocument)</p>
  
  <pre><code>public static XmlDocument CreateXmlDocument(string number, string email)
  {
      #region root Node
      XmlDocument xmlDoc = new XmlDocument();
      XmlNode rootNode = xmlDoc.CreateElement("methodCall");
      xmlDoc.AppendChild(rootNode);
      #endregion
  
      #region XML Node Method name
      XmlNode methodNameNode = xmlDoc.CreateElement("methodName");
      methodNameNode.InnerText = "customer.send";
      rootNode.AppendChild(methodNameNode);
      #endregion
  
      #region XML Parameters
      XmlNode paramsNode = xmlDoc.CreateElement("params");
  
      #region Setting Number
      XmlNode paramElement = xmlDoc.CreateElement("param");
      XmlNode valueAttribute = xmlDoc.CreateElement("value");
      XmlNode stringAttribute = xmlDoc.CreateElement("string");
      stringAttribute.InnerText = number;
      valueAttribute.AppendChild(stringAttribute);
      paramElement.AppendChild(valueAttribute);
      #endregion
  
      #region Setting Email
      XmlNode paramElement2 = xmlDoc.CreateElement("param");
      XmlNode valueAttribute2 = xmlDoc.CreateElement("value");
      XmlNode stringAttribute2 = xmlDoc.CreateElement("string");
      stringAttribute2.InnerText = email;
      valueAttribute2.AppendChild(stringAttribute2);
      paramElement2.AppendChild(valueAttribute2);
      #endregion
  
      #region Setting validation
      XmlNode paramElement3 = xmlDoc.CreateElement("param");
      XmlNode valueAttribute3 = xmlDoc.CreateElement("value");
      XmlNode stringAttribute3 = xmlDoc.CreateElement("i4");
      stringAttribute3.InnerText = "5";
      valueAttribute3.AppendChild(stringAttribute3);
      paramElement3.AppendChild(valueAttribute3);
      #endregion
  
      #region Adding Number, Email and validation to paramsNode
      paramsNode.AppendChild(paramElement);
      paramsNode.AppendChild(paramElement2);
      paramsNode.AppendChild(paramElement3);
      rootNode.AppendChild(paramsNode);
      #endregion
  
      #endregion
  
      #region Setting encoding to UTF8
      XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", null, null);
      xmlDeclaration.Encoding = "UTF-8";
      //xmlDec.Standalone = "yes";
      XmlElement root = xmlDoc.DocumentElement;
      xmlDoc.InsertBefore(xmlDeclaration, root);
      #endregion
  
      return xmlDoc;
  
  }
  </code></pre>
  
  <p>This will generate a XML like this</p>
  
  <p><a href="https://i.stack.imgur.com/wyrTc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wyrTc.png" alt="enter image description here"></a></p>
  
  <p><strong>2. Use the method and convert the XmlDocument to send it to the API</strong></p>
  
  <pre><code>// Create a new XML document with the give values
  XmlDocument xmlRequest = CreateXmlDocument(number, email);
  
  // Convert the Xml to a byte array for the HTTPS
  byte[] requestData = Encoding.ASCII.GetBytes(xmlRequest.InnerXml.ToString());
  
  // Define the request
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("UrlToHttpsApi");
  request.Method = "POST";
  request.ContentType = "text/xml";
  request.ContentLength = requestData.Length;
  request.ServerCertificateValidationCallback = delegate { return true; }; //only needed to allow self-signed certificates. If possible don't use this
  
  using (Stream requestStream = request.GetRequestStream())
      requestStream.Write(requestData, 0, requestData.Length);
  
  string result = null;
  
  // Send XML to the API
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
      using (Stream stream = response.GetResponseStream())
      {
          using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
              result = reader.ReadToEnd();
      }
  }
  </code></pre>
  
  <p><strong>3. Work with the result (just as example)</strong></p>
  
  <pre><code>// Serialize the result as XML
  XmlDocument xmlResponse = new XmlDocument();
  xmlResponse.LoadXml(result);
  </code></pre>
  
  <p>Maybe it's not the best solution. But it does the job like it should.</p>