GPGVM
GPGVM

Reputation: 5619

Error Generating XML Document from object

I am wishing to serialize a complex object for returning from a web service request. Here are my assumptions. I need to have the serialized (deflated) object in an XML document (as opposed to a string) before returning to the calling client. I "believe" I am deserializing just fine it is just a matter of getting it loaded into the XMLDocument. However I could be wrong and the deserialization may be wrong therefore the XmlDocument blows up. Here is the code:

My Complex Object:

namespace ABCTest
{
    [XmlRoot("TapRoot")]
    public class UserDetails
    {
        [XmlElement]
        public String AccountName { get; set; }
    }
}

My serialization code:

FYi: UsrDtls == List<UserDetails>

XmlSerializer Obj2XML = new XmlSerializer(UsrDtls.GetType());
Stream strWriter = Stream.Null;
XmlWriter XWriter = new XmlTextWriter(strWriter, Encoding.Unicode);
XmlDocument XDoc = new XmlDocument();

Obj2XML.Serialize(XWriter, lst_Exercises);
string abc = Obj2XML.ToString(); //debugging line to attempt to browse the obj2xml object
XDoc.LoadXml(abc);

return XDoc;

Upvotes: 0

Views: 483

Answers (1)

John Saunders
John Saunders

Reputation: 161773

I have no idea where you learned about web services in .NET. Just return the object. The web service infrastructure will take care of it.

You don't say whether you're using WCF services or the legacy ASMX services. The ASMX services should not be used for new development.

If you still have trouble when you "just return it", then please post the details of any exceptions you receive.

Upvotes: 1

Related Questions