Solved

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

Posted on 2006-06-30
6
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF user control poped up from a window 1 35
MediaWiki - Move a site 4 33
"The resource cannot be found" 1 24
c#  FTP ftpwebrequest URI invalid 6 16
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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…

696 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