private void InvokeUsingWsdl(XmlDocument wsdl) { XmlNode targetNamespaceNode = wsdl.DocumentElement.Attributes.GetNamedItem("targetNamespace"); string targetNamespace = targetNamespaceNode == null ? string.Empty : targetNamespaceNode.Value; NamespacePrefixes namespacePrefixes = new NamespacePrefixes(targetNamespace); string parametersXml = GetParametersXml(wsdl, namespacePrefixes); string soapRequest = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <" + _method + @" xmlns=""" + targetNamespace + @"""" + namespacePrefixes.GetPrefixDefinitions() + ">" + parametersXml + @"</" + _method + @"> </soap:Body> </soap:Envelope>"; jQueryAjaxOptions options = new jQueryAjaxOptions { DataType = "xml" }; bool useCredentials = !string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password); if (useCredentials) { options.Username = _username; options.Password = _password; } options.BeforeSend = request => { if (useCredentials) { request.SetRequestHeader("Authorization", "Basic " + StringUtility.ToBase64(_username + ":" + _password)); } string soapAction = FindSoapAction(wsdl, targetNamespace); request.SetRequestHeader("SOAPAction", soapAction); request.SetRequestHeader("Content-Type", "text/xml; charset=utf-8"); }; options.Timeout = (int)_timeout.TotalMilliseconds; options.Type = "POST"; jQueryAjaxOptionsUtility.SetDataString(options, soapRequest); jQuery.Ajax(_url, options).Then((data, textStatus, request) => _successCallback(GetReturnValue(wsdl, Script.Reinterpret<XmlDocument>(data)), textStatus, request), new AjaxFailCallback(_errorCallback)); }
private string GetParametersXml(XmlDocument wsdl, NamespacePrefixes namespacePrefixes) { string messageName = XPath.Evaluate(wsdl, ".//wsdl:portType/wsdl:operation[@name='" + _method + "']/wsdl:input/@message", GetNamespaceResolver(null))[0].Value; if (messageName.Contains(":")) { messageName = messageName.Substring(messageName.IndexOf(':') + 1); } string parametersElementFullName = XPath.Evaluate(wsdl, ".//wsdl:message[@name='" + messageName + "']/wsdl:part[@name='parameters']/@element", GetNamespaceResolver(null))[0].Value; string[] parametersElementNameParts = parametersElementFullName.Split(':'); string parametersElementNamespace; string parametersElementName; if (parametersElementNameParts.Length == 2) { parametersElementNamespace = XPath.Evaluate(wsdl, ".//wsdl:definitions/@xmlns:" + parametersElementNameParts[0], GetNamespaceResolver(null))[0].Value; parametersElementName = parametersElementNameParts[1]; } else { parametersElementNamespace = XPath.Evaluate(wsdl, ".//wsdl:definitions/@targetNamespace", GetNamespaceResolver(null))[0].Value; parametersElementName = parametersElementNameParts[0]; } IXmlSchemaElementDefinition elementDefinition = XmlSchemaParser.GetElementDefinition(wsdl, parametersElementNamespace, parametersElementName); string xml = string.Empty; foreach (IXmlSchemaElementDefinition childElementDefinition in ((XmlSchemaComplexTypeDefinition)elementDefinition.Type).Elements) { xml += GetObjectXmlRecursive(childElementDefinition, _parameters[childElementDefinition.Name], false, namespacePrefixes); } return xml; }
private string GetObjectXmlRecursive(IXmlSchemaElementDefinition elementDefinition, object value, bool ignoreArray, NamespacePrefixes namespacePrefixes) { string prefix = null; if (Script.IsNullOrUndefined(value)) { prefix = namespacePrefixes.GetPrefixAndColon(elementDefinition.ElementNamespace); return "<" + prefix + elementDefinition.Name + " i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />"; } string xml = string.Empty; IXmlSchemaSimpleTypeDefinition simpleTypeDefinition = elementDefinition.Type as IXmlSchemaSimpleTypeDefinition; if (simpleTypeDefinition != null) { switch (simpleTypeDefinition.Type) { case XmlBuiltInSimpleType.AnyType: xml += EncodeString(value.ToString()); break; case XmlBuiltInSimpleType.AnyUri: case XmlBuiltInSimpleType.Guid: case XmlBuiltInSimpleType.QName: case XmlBuiltInSimpleType.String: case XmlBuiltInSimpleType.Enum: EnsureScriptType(elementDefinition.Name, value, "string"); xml += EncodeString(Script.Reinterpret<string>(value)); break; case XmlBuiltInSimpleType.Base64Binary: string scriptType = Script.TypeOf(value); if (scriptType == "string") { xml += StringUtility.ToBase64(Script.Reinterpret<string>(value)); } else { EnsureJsType(elementDefinition.Name, value, "Array"); xml += StringUtility.ToBase64(Script.Reinterpret<byte[]>(value)); } break; case XmlBuiltInSimpleType.Boolean: EnsureScriptType(elementDefinition.Name, value, "boolean"); xml += value.ToString(); break; case XmlBuiltInSimpleType.Char: EnsureScriptType(elementDefinition.Name, value, "number"); xml += EncodeString(string.FromCharCode(Script.Reinterpret<char>(value))); break; case XmlBuiltInSimpleType.Byte: case XmlBuiltInSimpleType.Decimal: case XmlBuiltInSimpleType.Double: case XmlBuiltInSimpleType.Float: case XmlBuiltInSimpleType.Int: case XmlBuiltInSimpleType.Long: case XmlBuiltInSimpleType.Short: case XmlBuiltInSimpleType.UnsignedByte: case XmlBuiltInSimpleType.UnsignedInt: case XmlBuiltInSimpleType.UnsignedLong: case XmlBuiltInSimpleType.UnsignedShort: EnsureScriptType(elementDefinition.Name, value, "number"); xml += value.ToString(); break; case XmlBuiltInSimpleType.DateTime: if (!(value is DateTime)) { ThrowTypeException(elementDefinition.Name, value, typeof(DateTime)); } string year = Script.Reinterpret<DateTime>(value).GetFullYear().ToString(); string month = (Script.Reinterpret<DateTime>(value).GetMonth() + 1).ToString().PadLeft(2, '0'); string date = Script.Reinterpret<DateTime>(value).GetDate().ToString().PadLeft(2, '0'); string hours = Script.Reinterpret<DateTime>(value).GetHours().ToString().PadLeft(2, '0'); string minutes = Script.Reinterpret<DateTime>(value).GetMinutes().ToString().PadLeft(2, '0'); string seconds = Script.Reinterpret<DateTime>(value).GetSeconds().ToString().PadLeft(2, '0'); string milliseconds = Script.Reinterpret<DateTime>(value).GetMilliseconds().ToString(); int timezoneOffsetTotalMinutes = Script.Reinterpret<DateTime>(value).GetTimezoneOffset(); int timezoneOffsetTotalMinutesAbs = Math.Abs(timezoneOffsetTotalMinutes); int timezoneOffsetHours = timezoneOffsetTotalMinutesAbs/60; int timezoneOffsetMinutes = timezoneOffsetTotalMinutesAbs%60; string timezone = ((timezoneOffsetTotalMinutes < 0) ? "+" : "-") + timezoneOffsetHours.ToString().PadLeft(2, '0') + ":" + timezoneOffsetMinutes.ToString().PadLeft(2, '0'); xml += year + "-" + month + "-" + date + "T" + hours + ":" + minutes + ":" + seconds + "." + milliseconds + timezone; break; case XmlBuiltInSimpleType.Duration: if (!(value is TimeSpan)) { ThrowTypeException(elementDefinition.Name, value, typeof(TimeSpan)); } TimeSpan ts = Script.Reinterpret<TimeSpan>(value); bool durationIsNegative = ts.Ticks < 0; int durationDays = Math.Abs(ts.Days); int durationHours = Math.Abs(ts.Hours); int durationMinutes = Math.Abs(ts.Minutes); int durationSeconds = Math.Abs(ts.Seconds); int durationMilliseconds = Math.Abs(ts.Milliseconds); xml += (durationIsNegative ? "-" : string.Empty) + durationDays + "DT" + durationHours + "H" + durationMinutes + "M" + durationSeconds + "." + durationMilliseconds.ToString().PadLeft(3, '0') + "S"; break; default: throw UnhandledEnumValueExceptionFactory.Create(simpleTypeDefinition.Type); } } else { IXmlSchemaComplexTypeDefinition complexTypeDefinition = elementDefinition.Type as IXmlSchemaComplexTypeDefinition; if (complexTypeDefinition == null) { throw new NotSupportedException("Unknown type for element definition: " + elementDefinition.GetType().FullName); } bool isArrayContainer = IsArrayContainer(complexTypeDefinition); if (!ignoreArray && (isArrayContainer || elementDefinition.IsArray)) { if (!(value is IEnumerable)) { ThrowTypeException(elementDefinition.Name, value, typeof(IEnumerable)); } IXmlSchemaElementDefinition arrayElementDefinition; if (isArrayContainer) { prefix = namespacePrefixes.GetPrefixAndColon(elementDefinition.ElementNamespace); xml += "<" + prefix + elementDefinition.Name + ">"; arrayElementDefinition = complexTypeDefinition.Elements[0]; } else { arrayElementDefinition = elementDefinition; } foreach (object item in Script.Reinterpret<Array>(value)) { xml += GetObjectXmlRecursive(arrayElementDefinition, item, true, namespacePrefixes); } if (isArrayContainer) { xml += "</" + prefix + elementDefinition.Name + ">"; } return xml; } foreach (IXmlSchemaElementDefinition childElementDefinition in complexTypeDefinition.Elements) { xml += GetObjectXmlRecursive(childElementDefinition, Script.Reinterpret<JsDictionary>(value)[childElementDefinition.Name], false, namespacePrefixes); } } prefix = namespacePrefixes.GetPrefixAndColon(elementDefinition.ElementNamespace); return "<" + prefix + elementDefinition.Name + ">" + xml + "</" + prefix + elementDefinition.Name + ">"; }
internal static string toString(XmlElement element, NamespacePrefixes namespaces) { throw new System.NotSupportedException(); }
/// <summary> /// Add prefixes and namespace names for use in XPath expressions refering to that prefix. /// </summary> /// <param name="prefixNamespaceMap">map of prefixes and namespaces</param> public void AddNamespacePrefixes(IDictionary<string, string> prefixNamespaceMap) { NamespacePrefixes.PutAll(prefixNamespaceMap); }
/// <summary> /// Add a prefix and namespace name for use in XPath expressions refering to that prefix. /// </summary> /// <param name="prefix">is the prefix of the namespace</param> /// <param name="namespace">is the namespace name</param> public void AddNamespacePrefix( string prefix, string @namespace) { NamespacePrefixes.Put(prefix, @namespace); }