views:

101

answers:

3

Hello, I have a basic Generic List that I want turned into XML so I can return it to jquery. What I am trying to do is update my comments section in my article directory. I am returning an array of comment text, comment id, and user name. I would like to turn all of this into an array. Thanks

if (CommentFunctions.AddComment(aid, l.GetUserID(), id, comment))
        {
            //lets get all the comments for the article
            List<CommentType> ct = CommentFunctions.GetCommentsByArticleID(id);
        }
A: 

You have to serialize it to XML. There are a number of ways to do this, more or less complex depending on the relative efficiency/speed you need, and the amount of control you need over the XML output.

Have a look here:

http://msdn.microsoft.com/en-us/library/ms950721.aspx

Robert Harvey
A: 

As Robert's comment mentions, you have to serialize the array to XML. Instead of retyping out the answer, however, I would recommend reading this blog post which discusses exactly how you would go about doing that.

JasCav
+1  A: 

As others have pointed out, you'll need to serialize it to convert to XML.

I'd like to mention that if you're trying to return a list of objects to JQuery, that XML isn't the best or easiest format. Have you considered returning JSON?

JavaScriptSerializer serializer = new JavaScriptSerializer();
string JSONText = serializer.Serialize(List<CommentType>);

This will automatically create necessary json to describe your list of CommentTypes. JSON is much easier to parse in javascript and is much smaller to return via HTML.

Plus, you don't need to tell it your field names. It will find them for you and your JSON will be a list of classes just like your CommentType class.

Michael La Voie