Solved

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

Posted on 2007-11-21
15
348 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
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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

911 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

18 Experts available now in Live!

Get 1:1 Help Now