Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6004865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:22+00:00 2026-05-23T01:19:22+00:00

I have a web service which returns something of type MyData . public class

  • 0

I have a web service which returns something of type MyData.

 public class MyData
 {
        public string Name;
        [XmlElement("item")]
        public Object[] DataItems;
 }

I have used Object[] for DataItems because the type of array could be of several types. I have two different classes which I could successfully send using this method. See below.

clientResults is the filled DataSet.

MyData returnResult = new MyData();

MyFirstClass[] resultData = new MyFirstClass[clientResults.Tables[0].Rows.Count];

resultData.MyFirstClassProperty1 = "Blah Blah";
resultData.MyFirstClassProperty2 = "Blah Blah";

returnResult.DataItems = resultData.

I could easily change MyFirstClass to MySecondClass and set its own properties and the web service would properly serialize both the classes and every one was happy!

However now there is a need where I have to pass an XML returned by the DataSet.GetXml() function.

Naturally, what I did was

 XmlDocument xdoc = new XmlDocument();
 xdoc.LoadXml(clientResults.GetXml());
 resultData.DataItems = new XmlDocument[] { xdoc };

But this is throwing an exception

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Xml.XmlDocument may not be used in this context.

So what I thought, ok lets try it with XmlNode.

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(clientResults.GetXml());
XmlNode xElement = xdoc.SelectSingleNode("/");
result.DataItems = new XmlNode[] { xElement };

Still its throwing the SAME exception. What could be wrong?

How do I properly pass an XML through a web service?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:19:23+00:00Added an answer on May 23, 2026 at 1:19 am

    The answer was pretty easy. All I had to do was create a parent class which other classes were going to inherit from.

    public class BaseData
    {
    
    }
    
    public class XmlData : BaseData
    {
       public XmlNode xml;
    }
    

    And I made the Object[] to a BaseData[].

    public class MyData
     {
            public string Name;
            [XmlElement("item")]
            public BaseData[] DataItems;
     }
    

    And then I selected the node using XPath and assigned it.

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(clientResults.GetXml());
    XmlNode xElement = xdoc.SelectSingleNode("/");
    
    XmlData[] xmlData = new XmlData[1];
    xmlData[0] = new XmlData();
    xmlData[0].xml = xElement;
    
    result.DataItems = xmlData;
    

    I also had to put a XmlInclude(typeof(XmlData)) to the web service method signature.

    And it was working perfectly!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service which is running fine when i call it from
I have some WSDL from which I need to generate a web service implementation.
Oh hello there, fellow SO members, I have a web service that returns XML
I have a SpringMVC web service for uploading files which looks like this: @RequestMapping(value=/upload.json,
I am looking for a way to have the generated proxy class for a
I'm developing a Grails (Version 1.3.3) Web-Application using the Grails Spring-Security Plugin, Spring-Security-Core-1.0.1 (which,
I have the following code which lets me execute a workflow. This could be
I having some problems implementing IOC using windsor. I have several different implementations of
I'd like to implement a method which allows me to access a property of
I am a newbie with c# and window form I am doing a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.