/// <summary> /// Encodes a call of the specified <paramref name="action"/> with the given <paramref name="inParamValues"/> and /// returns the resulting SOAP XML string. /// </summary> /// <param name="action">Action to be called.</param> /// <param name="inParamValues">List of parameter values which must match the action's signature. /// Can be <c>null</c> if the parameter list is empty.</param> /// <param name="upnpVersion">UPnP version to use for the encoding.</param> /// <returns>XML string which contains the SOAP document.</returns> public static string EncodeCall(CpAction action, IList <object> inParamValues, UPnPVersion upnpVersion) { bool targetSupportsUPnP11 = upnpVersion.VerMin >= 1; StringBuilder result = new StringBuilder(5000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, true); writer.WriteStartElement("u", action.Name, action.ParentService.ServiceTypeVersion_URN); // Check input parameters IList <CpArgument> formalArguments = action.InArguments; if (inParamValues == null) { inParamValues = EMPTY_OBJECT_LIST; } if (inParamValues.Count != formalArguments.Count) { throw new ArgumentException("Invalid argument count"); } for (int i = 0; i < formalArguments.Count; i++) { CpArgument argument = formalArguments[i]; object value = inParamValues[i]; writer.WriteStartElement(argument.Name); argument.SoapSerializeArgument(value, !targetSupportsUPnP11, writer); writer.WriteEndElement(); // argument.Name } SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return(result.ToString()); }
protected static string CreateResultDocument(DvAction action, IList <OutParameter> outParameters, bool forceSimpleValues) { StringBuilder result = new StringBuilder(2000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, true); writer.WriteStartElement("u", action.Name + "Response", action.ParentService.ServiceTypeVersion_URN); foreach (OutParameter parameter in outParameters) { writer.WriteStartElement(parameter.Argument.Name); parameter.Argument.SoapSerializeArgument(parameter.Value, forceSimpleValues, writer); writer.WriteEndElement(); // parameter.Argument.Name } writer.WriteEndElement(); // u:[action.Name]Response SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return(result.ToString()); }
public static string CreateFaultDocument(uint errorCode, string errorDescription) { StringBuilder result = new StringBuilder(2000); using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(result, UPnPConsts.UTF8_NO_BOM)) using (XmlWriter writer = XmlWriter.Create(stringWriter, UPnPConfiguration.DEFAULT_XML_WRITER_SETTINGS)) { SoapHelper.WriteSoapEnvelopeStart(writer, false); writer.WriteStartElement("Fault", UPnPConsts.NS_SOAP_ENVELOPE); string soapNamespacePrefix = writer.LookupPrefix(UPnPConsts.NS_SOAP_ENVELOPE); writer.WriteElementString("faultcode", soapNamespacePrefix + ":Client"); writer.WriteElementString("faultstring", "UPnPError"); writer.WriteStartElement("detail"); writer.WriteStartElement(string.Empty, "UPnPError", UPnPConsts.NS_UPNP_CONTROL); writer.WriteElementString("errorCode", errorCode.ToString()); writer.WriteElementString("errorDescription", errorDescription); writer.WriteEndElement(); // UPnPError writer.WriteEndElement(); // detail writer.WriteEndElement(); // s:Fault SoapHelper.WriteSoapEnvelopeEndAndClose(writer); } return(result.ToString()); }