469,894 Members | 2,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,894 developers. It's quick & easy.

Passing XML to web service?

I want to pass some xml to a web service method, the xml will confirm to a
defined schema. Lets say the schema is defined as:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

and a sample xml fragment is:

<books>
<book>
<name>Harry Potter</name>
<price>$12.99</price>
</book>
<book>
<name>Business at the speed of thought</name>
<price>$10.00</price>
</book>
</books>

What is the best way of passing the xml into the webmethod? Is it possible
to mark the method so the WSDL incorporate the schema?

Thanks

Oct 31 '06 #1
6 13595
Use xsd.exe to generate types for the schema, then design your web service
to pass those generated types arounds via the web methods.

xsd.exe myschema.xsd /c

to generate C# classes for the schema.

The important thing here is you want the underlying infrastructure to handle
the serialization from xml to object, and vice versa.

Ron

"nickname" <ni*******@newsgroups.nospamwrote in message
news:FD**********************************@microsof t.com...
>I want to pass some xml to a web service method, the xml will confirm to a
defined schema. Lets say the schema is defined as:

<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

and a sample xml fragment is:

<books>
<book>
<name>Harry Potter</name>
<price>$12.99</price>
</book>
<book>
<name>Business at the speed of thought</name>
<price>$10.00</price>
</book>
</books>

What is the best way of passing the xml into the webmethod? Is it possible
to mark the method so the WSDL incorporate the schema?

Thanks

Oct 31 '06 #2
Thanks for Ron's input.

Hi Nick,

Based on your description, I think what you want is make your webservice's
request and response SOAP message's output XML conform to your predefined
XML schema so that the client can send predefiend XML format content to the
server service no matter what platform or component client is using,
correct?

As for the server-side webservice, are you developing it through ASP.NET
asmx webservice? If so, I think you can consider use "Contract-First"
develop pattern to create your webservice. It is the most recommended way
to develop ASP.NET webservice that will output specified format XML message
for interopability purpose. Here is the steps of Cotract-First development:

** author the business class's XML Schema(XSD), you have already defined it

** use tools(like the XSD.exe Ron mentioned) or manually define .net
classes which mapping the XML schema you defined (by XML Serialization
support in .net framework).

** customize the asmx webservice's webmethod (use
SoapDocumentMethodAttribute) and set the webmethods to use those XML
serlization mapped classes defined ealier, thus ,the webmethod's SOAP
output message will conform to the expected XML format(XML schema)

** Call the webservice or check WSDL to verify that the output XML content
is as expected

Here are some good tech articles that introducing Contract-First service
development and how it works in ASP.NET asmx webservice:
#Contract-First Service Development
http://msdn.microsoft.com/msdnmag/is...n/default.aspx

#Techniques for Contract-First Development
http://msdn.microsoft.com/msdnmag/is...erviceStation/

#Enrich Your XML Serialization With Schema Providers In The .NET Framework
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

In addition, if you are not quite familar with XML serialization technology
in .net framework, you can have a look at the XML serialization topics in
MSDN:

#XML Serialization in the .NET Framework
http://msdn.microsoft.com/library/en...03.asp?frame=t
rue

#Introducing XML Serialization
http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx

Hope this helps. Please feel free to post here if you have anything unclear
on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 1 '06 #3
Thanks for the replies. I have generated a c# class for the schema as Ron
described. I am unsure as to how to use the SoapDocumentMethodAttribute?

[WebMethod]
public void test(booksBook inputXML)
{
}

It is my understanding that i can use something like the above and mark it
with the
SoapDocumentMethodAttribute and it will do as i require? or do i need to
write WSDL myself as decribed in the web sites mentioned?

Thanks

"Steven Cheng[MSFT]" wrote:
Thanks for Ron's input.

Hi Nick,

Based on your description, I think what you want is make your webservice's
request and response SOAP message's output XML conform to your predefined
XML schema so that the client can send predefiend XML format content to the
server service no matter what platform or component client is using,
correct?

As for the server-side webservice, are you developing it through ASP.NET
asmx webservice? If so, I think you can consider use "Contract-First"
develop pattern to create your webservice. It is the most recommended way
to develop ASP.NET webservice that will output specified format XML message
for interopability purpose. Here is the steps of Cotract-First development:

** author the business class's XML Schema(XSD), you have already defined it

** use tools(like the XSD.exe Ron mentioned) or manually define .net
classes which mapping the XML schema you defined (by XML Serialization
support in .net framework).

** customize the asmx webservice's webmethod (use
SoapDocumentMethodAttribute) and set the webmethods to use those XML
serlization mapped classes defined ealier, thus ,the webmethod's SOAP
output message will conform to the expected XML format(XML schema)

** Call the webservice or check WSDL to verify that the output XML content
is as expected

Here are some good tech articles that introducing Contract-First service
development and how it works in ASP.NET asmx webservice:
#Contract-First Service Development
http://msdn.microsoft.com/msdnmag/is...n/default.aspx

#Techniques for Contract-First Development
http://msdn.microsoft.com/msdnmag/is...erviceStation/

#Enrich Your XML Serialization With Schema Providers In The .NET Framework
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

In addition, if you are not quite familar with XML serialization technology
in .net framework, you can have a look at the XML serialization topics in
MSDN:

#XML Serialization in the .NET Framework
http://msdn.microsoft.com/library/en...03.asp?frame=t
rue

#Introducing XML Serialization
http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx

Hope this helps. Please feel free to post here if you have anything unclear
on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights
Nov 1 '06 #4
I'm not sure I follow where you are with this, so I'll just describe my
understanding of it from the beggining.

You have an existing xml schema for books.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

You've used xsd.exe to generate some .NET classes based on that schema, and
got a file with:

partial class books{}
and
partial class booksBook{}

Where "books" is a collection of "booksBook"

You've developed an ASMX Web service:

public class MyService : WebService
{
[WebMethod]
public void test(books inputXml){}
}

Now, for clients, any client capable of consuming web services, they merely
1) create a client side proxy using the WSDL for that web service, 2)
populate the booksBook object that will be created when the proxy is
created, and 3) send that object to the service. The infrastructure of the
platform will do the xml serialization of the bookBook object to a SOAP
message whose body is in the books.xsd defined format.

Does that not help any?

Ron

"nickname" <ni*******@newsgroups.nospamwrote in message
news:5F**********************************@microsof t.com...
Thanks for the replies. I have generated a c# class for the schema as Ron
described. I am unsure as to how to use the SoapDocumentMethodAttribute?

[WebMethod]
public void test(booksBook inputXML)
{
}

It is my understanding that i can use something like the above and mark it
with the
SoapDocumentMethodAttribute and it will do as i require? or do i need to
write WSDL myself as decribed in the web sites mentioned?

Thanks

"Steven Cheng[MSFT]" wrote:
>Thanks for Ron's input.

Hi Nick,

Based on your description, I think what you want is make your
webservice's
request and response SOAP message's output XML conform to your predefined
XML schema so that the client can send predefiend XML format content to
the
server service no matter what platform or component client is using,
correct?

As for the server-side webservice, are you developing it through ASP.NET
asmx webservice? If so, I think you can consider use "Contract-First"
develop pattern to create your webservice. It is the most recommended way
to develop ASP.NET webservice that will output specified format XML
message
for interopability purpose. Here is the steps of Cotract-First
development:

** author the business class's XML Schema(XSD), you have already defined
it

** use tools(like the XSD.exe Ron mentioned) or manually define .net
classes which mapping the XML schema you defined (by XML Serialization
support in .net framework).

** customize the asmx webservice's webmethod (use
SoapDocumentMethodAttribute) and set the webmethods to use those XML
serlization mapped classes defined ealier, thus ,the webmethod's SOAP
output message will conform to the expected XML format(XML schema)

** Call the webservice or check WSDL to verify that the output XML
content
is as expected

Here are some good tech articles that introducing Contract-First service
development and how it works in ASP.NET asmx webservice:
#Contract-First Service Development
http://msdn.microsoft.com/msdnmag/is...n/default.aspx

#Techniques for Contract-First Development
http://msdn.microsoft.com/msdnmag/is...erviceStation/

#Enrich Your XML Serialization With Schema Providers In The .NET
Framework
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

In addition, if you are not quite familar with XML serialization
technology
in .net framework, you can have a look at the XML serialization topics in
MSDN:

#XML Serialization in the .NET Framework
http://msdn.microsoft.com/library/en...03.asp?frame=t
rue

#Introducing XML Serialization
http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx

Hope this helps. Please feel free to post here if you have anything
unclear
on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

================================================= =

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

================================================= =

This posting is provided "AS IS" with no warranties, and confers no
rights

Nov 2 '06 #5
Thanks for Ron's further input.

Hi Nick,

Yes, after you generate the class(that maps your XSD schema) and configure
webmethod to use it as below
===========
[WebMethod]
public void test(booksBook inputXML)
{
}
==========

the webservice will serialize the inputXML(of booksBook type) into the XML
format that conform to the schema. However, by default ASP.NET webservice
will add some additional wrapper xml element around each webmethod's
paramter or return type. You can use additional attribute option to
customize this (like the SoapDocumentMethodAttrirbuet). e.g.

=====================
[WebMethod()]
[SoapDocumentMethod(
"http://msdn.microsoft.com/purchasing:GetPending",
Use= SoapBindingUse.Literal, ParameterStyle=
SoapParameterStyle.Bare)]
[return: XmlElement("GetPendingResponse",
Namespace="http://msdn.microsoft.com/purchasing")]
public override GetPendingResponse GetPending([XmlElement(
Namespace="http://msdn.microsoft.com/purchasing")]
GetPendingRequest GetPendingRequest)
{
... // put your code here
}
============================

the following article I mentioned in the last reply has well described this:

http://msdn.microsoft.com/msdnmag/is...erviceStation/

Actually, when using contract-first approach, you can even first create the
WSDL document and use WSDL.exe (with /serverinterface option) to create
ASP.NET webservice interface code that conform to the WSDL document:

#Web Services Description Language Tool (Wsdl.exe)
http://msdn2.microsoft.com/en-us/library/7h3ystb6.aspx

You can try those parameterstyle options and run the webservice to see the
SOAP message content(use some http or tcp trace tools), thus, you can get a
clear view of the difference of the various paramter styles.

Please feel free to let me know if you have anything unclear about it.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 2 '06 #6
Hi Steven,

Just some remarks on wsdl.exe. I've recently produced a demo where as much
as possible I stayed out of Visual Studio to define the web service
definition - using XML Spy for the .xsd and .wsdl. Then wsdl.exe, and
wsewsdl3.exe to generate the service code. The whole approach intrigued me,
as in the past I mostly defined service interfaces using the code-first
approach.

One thing I noticed is when I generated the client proxy using wsdl.exe, the
resulting code file seemed more "natural". That is, when I generated the
proxy, I got a code file with this class:

public class IODEFDocument
{
private Incident[] incident;
public Incident[] Incident{ get{ .. } }
}

public class Incident
{
private EventData[] eventData;
public EventData[] EventData{ get{ ... }}
}

and programming the client was relatively easy, and had a good feel to it.
When I generated the proxy via Add Web Reference, or using wsdl.exe to point
to http://mydomain.com/myservice.asmx?wsdl I would get something like this:

public class IODEFDocument
{
private IODEFDocumentIncident[] Incident;
...
}
public class IODEFDocumentIncident
{
private IODEFDocumentIncidentEventData[] EventData;
...
}

public class IODEFDocumentIncidentEventData
{
private IODEFDocumentIncidentEventDataFlow[] Flow;
}

As you can see, the class names were unexpected, and less than desirable to
program against. I used the IODEF specification published by the IETF at
http://xml.coverpages.org/draft-ietf-inch-iodef-10.txt

As I mentioned earlier, defining the WSDL outside of Visual Studio, and
using wsdl.exe to generate service side interface, and client side proxy
directly from the .wsdl file produced the desired results.

Borland's JBuilder will also generate the long class names when I point it
to .asmx?WSDL. As another example, here is perhaps the longest class name
generated:

public enum IODEFDocumentIncidentEventDataFlowSystemNodeAddres sCategory

Where the IODEFDocumentIncidentEventDataFlowSystemNodeAddres s class has a
member variable of that enumeration type. It's just ridiculous.

Ron

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:z3**************@TK2MSFTNGXA01.phx.gbl...
Thanks for Ron's further input.

Hi Nick,

Yes, after you generate the class(that maps your XSD schema) and configure
webmethod to use it as below
===========
[WebMethod]
public void test(booksBook inputXML)
{
}
==========

the webservice will serialize the inputXML(of booksBook type) into the XML
format that conform to the schema. However, by default ASP.NET webservice
will add some additional wrapper xml element around each webmethod's
paramter or return type. You can use additional attribute option to
customize this (like the SoapDocumentMethodAttrirbuet). e.g.

=====================
[WebMethod()]
[SoapDocumentMethod(
"http://msdn.microsoft.com/purchasing:GetPending",
Use= SoapBindingUse.Literal, ParameterStyle=
SoapParameterStyle.Bare)]
[return: XmlElement("GetPendingResponse",
Namespace="http://msdn.microsoft.com/purchasing")]
public override GetPendingResponse GetPending([XmlElement(
Namespace="http://msdn.microsoft.com/purchasing")]
GetPendingRequest GetPendingRequest)
{
... // put your code here
}
============================

the following article I mentioned in the last reply has well described
this:

http://msdn.microsoft.com/msdnmag/is...erviceStation/

Actually, when using contract-first approach, you can even first create
the
WSDL document and use WSDL.exe (with /serverinterface option) to create
ASP.NET webservice interface code that conform to the WSDL document:

#Web Services Description Language Tool (Wsdl.exe)
http://msdn2.microsoft.com/en-us/library/7h3ystb6.aspx

You can try those parameterstyle options and run the webservice to see the
SOAP message content(use some http or tcp trace tools), thus, you can get
a
clear view of the difference of the various paramter styles.

Please feel free to let me know if you have anything unclear about it.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 5 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Amr Mostafa | last post: by
2 posts views Thread by Bob | last post: by
22 posts views Thread by Arne | last post: by
4 posts views Thread by Steven | last post: by
2 posts views Thread by Hakan Örnek | last post: by
2 posts views Thread by =?Utf-8?B?bXVyYWRqYW1lcw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.