Russell Verdun wrote:
Yes, I did get your answer, but not sure where to start, I'm not
versed in XML, but it looks straight forward, where would XML code go?
Ahh, well we can help you out there. Unfortunately the answer to your question is a lot longer than could possibly be explained and understood in a newsgroup posting. I highly suggest reading up on XML, XML Schema and WSDL. However, I'll explain how I would do it from a pure .NET perspective and hopefully you can run with it from there.
I would suggest something like the following...
An schema like:
<xs:schema id="SimpleCommandExample" targetNamespace="uri:marsh-samples-SimpleCommand" elementFormDefault="qualified"
xmlns="uri:marsh-samples-SimpleCommand" xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified">
<xs:complexType name="SimpleCommand">
<xs:sequence>
<xs:element name="parameters" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element name="parameter" type="SimpleCommandParameter" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="text" type="xs:string" />
</xs:complexType>
<xs:complexType name="SimpleCommandParameter">
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="value" type="xs:string" />
<xs:attribute name="type" type="xs:QName" />
</xs:complexType>
<xs:element name="command" type="SimpleCommand"></xs:element>
</xs:schema>
Which would result in instance documents like:
<command xmlns="uri:marsh-samples-SimpleCommand" xmlns:xsd="http://www.w3.org/2001/XMLSchema" text="SELECT * FROM MyTable WHERE myField = @myParameter">
<parameters>
<parameter name="@myParameter" type="xsd:int" value="1" />
</parameters>
</command>
And a web method that handles such commands like:
<codeSnippet language="C#">
[WebMethod]
[SoapDocumentMethod(ParameterStyle=SoapParameterSty le.Bare, Use=SoapBindingUse.Literal)]
[return:XmlElement("results", Namespace="uri:marsh-samples-MyServiceTypes")]
public XmlDataDocument ExecuteCommand([XmlElement("command", Namespace="uri:marsh-samples-SimpleCommand")] XmlElement commandElement)
{
// TODO: run a validating reader over the element if you're concerned someone might pass you invalid instances
SqlCommand command = new SqlCommand(commandElement.GetAttribute("text"));
// NOTE: assumes schema is in default namespace which it probably wouldn't be, so make sure to use overload where
// XmlNamespaceManager is passed and use namespace prefix on XPath elements
XmlNodeList parameterElements = commandElement.SelectNodes("parameters/parameter");
if(parameterElements.Count > 0)
{
SqlParameterCollection commandParameters = command.Parameters;
foreach(XmlElement parameterElement in parameterElements)
{
string xsdNamespacePrefix = commandElement.OwnerDocument.GetPrefixOfNamespace( "http://www.w3.org/2001/XMLSchema");
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = parameterElement.GetAttribute("name");
string parameterElementValue = parameterElement.GetAttribute("value");
string parameterXsdType = parameterElement.GetAttribute("type");
string[] qNameParts = parameterXsdType.Split(':');
if(qNameParts[0] != xsdNamespacePrefix)
{
throw new NotSupportedException("Expecting parameter types only from the Xml Schema Definition namespace.");
}
object parameterValue;
switch(qNameParts[1])
{
case "int":
case "nonNegativeInteger":
parameterValue = XmlConvert.ToInt32(parameterElementValue);
break;
case "string":
parameterValue = parameterElementValue;
break;
// TODO: handle all the XSD types you want support
default:
throw new NotSupportedException(string.Format("Unsupported XSD type specified: {0}.", parameterXsdType));
}
parameter.Value = parameterValue;
}
}
// TODO: execute the command and get back a real, filled dataset
DataSet resultDataSet = new DataSet();
XmlDataDocument result = new XmlDataDocument(resultDataSet);
return result;
}
</codeSnippet>
HTH,
Drew