Dean Madden
Dean Madden

Reputation: 341

How to create a web service that receives and sends xml based on xsd files?

I need to create a .NET web service that accepts xml, uses this to query a database, and then returns xml. I have been given xsd files for the request and response. Could someone point me in the right direction for where I start from or an example?

I haven't used WCF before so would prefer to use a simple asmx file to do this. I know how to get the data from the database, so it's the xml and web service bits where I'm lost.

I've tried googling this for a while but don't know where to start. Thanks.

Upvotes: 1

Views: 10647

Answers (2)

tom redfern
tom redfern

Reputation: 31750

The problem you have is that asmx and WCF are both code-first web service technologies. What this means is that you generally start with classes and the web service stack takes care of exposing your types as XML across the wire.

You are starting with a schema, which is not code. So if you want to use asmx/wcf you need to model your schema in code. You can do this by inferring a class structure from your schema using xsd.exe (or svcutil.exe for WCF).

Alternatively you can model your classes by hand based on the schema definition.

Once you have your classes then you can add declarative attributes to the code (See http://msdn.microsoft.com/en-us/library/83y7df3e.aspx for asmx, DataContract and DataMember for WCF). These attributes control:

  1. how an incoming stream of XML is deserialized to a type when a service request is received, and
  2. how instances of your response types get serialized to XML when passed out of your service

The problem with this approach is that getting your XML to validate against your XSD schemas will be a little bit hit and miss, as you cannot rely 100% on class inference from XSD, and additionally you may miss some fine detail if you are modelling it by hand.

Whichever way you do it you need to make sure that your request and response class instances cleanly serialize into XML which will validate against the XSD schemas you have been given.

Also look at a framework called WSCF-Blue which allows you to do contract-first web service design: http://wscfblue.codeplex.com/

Good luck, if you need any more detail about this please let me know via a comment.

Upvotes: 5

Ali Khalid
Ali Khalid

Reputation: 1345

From what I can understand, you need to build a webservice, which will accept XML as input, do some processing and spit out XML.

I assume you have a basic understanding of XML but dont know anything about XSD. In very simple terms, XSD is a document which is used to validate a XML file. Think of it a rule book for how XML file should be structed, you can read more about XSD from W3schools. Dont worry about the XSD to much right now. Get a few sample XML documents, which you need to accept as input and output. Build a console application to parse the sample XML file and get the results from the database. Then use the results to build the output XML by looking at the output sample XML. Once you have that completed, then you can use the .NET classes to validate your input and output XML from the XSD you have.

You can look at this answer to see how validation is done.

Once that is done, you can create your web service to return the XML as string.

Hope this help.

Upvotes: 0

Related Questions