Link to home
Start Free TrialLog in
Avatar of ianinspain
ianinspain

asked on

Writing an inline schema for my XML which is returned from my web service.

Hi there,

I wonder if anybody can help, i need to return with my XML (from my webservice) an inline schema.

This is because i am using ReadXML in my program which takes the XML and converts to dataset, but currently because there is no inline schema ... all fields are imported as String.

currently this is an example of what is returned, as you can seen the field "TITLES" needs to be a Integer.. which i believe is possible if i specify an inline schema for my XML doc.

  <?xml version="1.0" encoding="utf-8" ?>
- <response>
  <Error code="0" description="OK" />
- <myData>
- <Record>
  <COD>abcdef</COD>
  <NAMED>Company A</NAMED>
  <TITLES>560</TITLES>
  </Record>
- <Record>
  <COD>dfggh</COD>
  <NAMED>Company B</NAMED>
  <TITLES>30000</TITLES>
  </Record>
  </myData>
  </response>

Basically i have created my XML file by simply, building it in a string called returnedXML and then

XmlDocument xmlDocumentObject = new XmlDocument();
xmlDocumentObject.LoadXml(returnedXML);

But to create an inline schema? How would i go about doing this? Is there some method for creating the schema from XmlDocument object?

Any help would be really appreciated. Thanks in advance

Ian


SOLUTION
Avatar of sumix
sumix

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devsolns
devsolns

I would NOT return XML or Datasets.  My approach below binds the type to the schema and generates WSDL that includes the schema constraints.  If you use XMLDocument as return type then it will show in WSDL is </any> and that is bad.  Instead use this approach which binds to an xml schema.  This or using WSCF is the very best approach for doing web services the right way.


[XmlSchemaProvider("GetCustomerSchema")]
[XmlRoot("Customer", Namespace = "http://tempuri.com/Customer/v1")]

public class Customer : IXmlSerializable
{
    public string FirstName;

    public string LastName;

    public DateTime DOB;

    public string SSN;

    //serialization code.

    private static readonly string ns = "http://tempuri.com/Customer/v1";

    #region IXmlSerializable Members

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader) { }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("FirstName", ns, FirstName);
        writer.WriteElementString("LastName", ns, LastName);
        writer.WriteElementString("DOB", ns, DOB.ToShortDateString());
        writer.WriteElementString("SSN", ns, SSN);
    }

    #endregion

    public static XmlQualifiedName GetCustomerSchema(XmlSchemaSet xs)
    {
        XmlSerializer schemaSerializer = new XmlSerializer(typeof(XmlSchema));

        string xsdPath = "CustomerSchema.xsd";

        XmlSchema s = (XmlSchema)schemaSerializer.Deserialize(
            new XmlTextReader(xsdPath), null);

        xs.XmlResolver = new XmlUrlResolver();

        xs.Add(s);

        return new XmlQualifiedName("Customer", ns);
    }
}
Put this code in and run it.  You'll see the magic when you look at the WSDL generated and how nice it works on the other side of the wire.  Also you'll get near perfect interop with non .NET languages this way.  Also the interface nicely.
Avatar of ianinspain

ASKER

hi devsolns,

i am excited about your answer but i am little but confused...

Are you saying that in my webservice that i should used XMLdocument?

I actually do this

[WebMethod(Description="Returns test data")]
            public XmlDocument Gettestdata(string codCliente)
            {

and on the other side i.e.the app that calls the webservice i set the retun type to a xmlNode so i can use xpath to interrogate it

Are you saying i should be doing a different way?

Thanks in advance

Ian
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem with returning an XMLDocument type is the user of your service/wsdl has an unknown interface.  All its going to say is the return is "some" xml document but i can tell you whats in it hence the reason you wanted to put in an inline schema.

The approach above solves both issues.  It provides the exact interface and what elements are included in the Customer object and it also includes the xml schema declaration in the wsdl.