Solved

Can a C# webservice have a single DataSet parameter to handle a multi-relational XML/SOAP post?

Posted on 2007-11-21
15
343 Views
Last Modified: 2013-11-26
I have a webpage that is accessing a C#/.NET webservice.  It sends some XML that has several children (and the children have children as well).  For example:
<data>
   <first_child_of_data>
       <first_child_of_first_child>
       </first_child_of_first_child>
   </first_child_of_data>
   <second_child_of_data>
       <first_child_of_second_child>
       <first_child_of_second_child>
   </second_child_of_data>
</data>

My webservice has a single function.  My question is on the webservice side, do i have to make my function have a parameter for each tag(or row) or can I collect the entire XML/Soap post in one DataSet parameter, like so:
    [WebMethod]
    public string XMLtoString(DataSet datastream) { }
 
And if so, my main goal is to take that XML post and return a single string like:
string returnstring = <data><first_child_of_data><first_child_of_first_child></first_child_of_first_child></first_child_of_data>   <second_child_of_data><first_child_of_second_child><first_child_of_second_child></second_child_of_data></data>";

Besides the yes or no answer, some sample code for the string building would be much appreciated.

Thanks
0
Comment
Question by:RichardRiga
  • 7
  • 5
  • 3
15 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
I would go the DataSet route, since it is serializable, and can bundle all the information that you need into a single entity.

Bob
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
You could have just one dataset instead of calling it for every row.
The dataset has a WriteXML method to convert the dataset to string(XML)
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
thanks for the quick responses, but can one of you show me *how* to do that?  I have the single DataSet parameter already and I'm using WriteXML but i'm messing something up.   thanks
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
>>but i'm messing something up
More details would help :)
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
If you are trying to create a custom XML document (because DataSet.WriteXml doesn't cut it), then you could create an XmlDocument and use methods like CreateElement.

Example:

Create a New XML File Using XmlDocument
http://www.devx.com/tips/Tip/21168

Bob
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
Below is my webmethod.  When I run the client I receive a series of numbers that have nothing to do with the data submitted.  It seems random but its the same numbers each time its run.  What I expect to see is all the tags and their values in a single string as posted originally.  Some extra info... if i use         return datastream.Tables.Count.ToString() I get a 0 as a return value.  I had thought that each of the children became tables of their own, but I guess not.

    [WebMethod]
    public string XMLtoString(DataSet datastream)
    {      
        string returnstring = "";
        byte[] arr;
       
        MemoryStream stream = new MemoryStream();      
        datastream.WriteXml(stream);
        arr = stream.ToArray();
       
        for (int ix = 0; ix < arr.Length; ix++)
        {
            returnstring += arr.GetValue(ix).ToString();
        }
         
        return returnstring;
  }
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you saying that the incoming DataSet doesn't have any tables attached?

Bob
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
Bob, right.

Say I submit the above sample XML via a SOAP post.  When it gets to the webmethod and i use the         return datastream.Tables.Count.ToString() i get a zero as a response.
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
Learned one, hi.

I'm not trying to create a custom XML document.

I guess, in a nutshell, i'm trying to have my webmethod be able accept ANY xml/soap post then send back a string representation of it, tags and values.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Hmmm, I am trying to understand the value of this web method.  Are you trying to create a different representation for the incoming XML text?

Bob
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
I am trying to send an XML/SOAP message to a webservice.  regardless of the number of children, grandchildren, etc, I'd like to have the webservice method accept it as a single DataSet parameter, then spit it back out as a string.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
How are you constructing the DataSet to pass into the web method?  Since DataSet.Tables.Count = 0, it sounds like it is not working very well.

Bob
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
What do you mean constructing?  I assumed that the XML post would come in as the DataSet.
0
 
LVL 1

Author Comment

by:RichardRiga
Comment Utility
like:
=============================
==<data>                                       ==
==   <first_child_of_data>             ==
==      <first_child_of_first_child>  ==    
==       </first_child_of_first_child>==                                                    
==   </first_child_of_data>             ==                                                          
==</data>                                       ==                                                                      
==============================
                     ||
                     ||
             to webservice
                    ||
                    ||
============================================
== [WebMethod]                                                         ==
==public string XMLtoString(DataSet datastream) { } ==
============================================
                  ||
                  ||
         returns string
==
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
You mean to say that you directly write the XML into the webservice rather than using a proxy?
You need not send it to the web service just to get a string. you could just use a function to convert it to string

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now