?
Solved

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

Posted on 2007-11-21
15
Medium Priority
?
388 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 2000 total points
ID: 20328333
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
ID: 20328348
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
ID: 20328393
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
Technology Partners: 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!

 
LVL 21

Expert Comment

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

Expert Comment

by:Bob Learned
ID: 20328517
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
ID: 20328571
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
ID: 20328608
Are you saying that the incoming DataSet doesn't have any tables attached?

Bob
0
 
LVL 1

Author Comment

by:RichardRiga
ID: 20328712
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
ID: 20328736
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
ID: 20328805
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
ID: 20328882
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
ID: 20328935
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
ID: 20328989
What do you mean constructing?  I assumed that the XML post would come in as the DataSet.
0
 
LVL 1

Author Comment

by:RichardRiga
ID: 20329039
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
ID: 20329134
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 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