Solved

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

Posted on 2006-06-30
6
388 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 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

22 Experts available now in Live!

Get 1:1 Help Now