Solved

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

Posted on 2006-06-30
6
389 Views
Last Modified: 2013-11-19
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


0
Comment
Question by:ianinspain
  • 4
6 Comments
 
LVL 12

Assisted Solution

by:sumix
sumix earned 250 total points
ID: 17017133

Here is an way you can solve this:
Create a dataset, infere its schema from the xml file, then save it:

DataSet ds = new DataSet();
ds.InferXmlSchema("doc.xml",null);
ds.WriteXmlSchema("doc.xsd");
 - this is done once.

You can edit this schema with vs .net (you'll have three tables created here, you'll probably only need to change Record table structure) and save it.

When you read these xml files you can do it like:

DataSet ds = new DataSet();
ds.ReadXmlSchema("doc.xsd");
ds.ReadXml("doc.xml");
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17018274
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);
    }
}
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17018292
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ianinspain
ID: 17019075
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
0
 
LVL 13

Accepted Solution

by:
devsolns earned 250 total points
ID: 17019158
No I'm saying you SHOULD NOT use XMLDocument/XMLNode as your return type.  The same goes for a DataSet.

Your implementation code along with the Customer class above would be used like,

    [WebMethod]
    public Customer GetCustomer()
    {
        Customer c = new Customer();
        c.FirstName = "gary";
        c.LastName = "paulish";
        c.DOB = DateTime.Now;
        c.SSN = "222001234";
        return c;
    }

    [WebMethod]
    public Customer SetCustomer(Customer customer)
    {
        return customer;
    }


The big different is now when .NET goes to serialize that into wsdl it will included and enforce the xml schema instead of going about its own thing.  With the Customer class above you are basically overriding the default serialization that would occur with the customer object and that is a GOOD thing!
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17019225
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now