6 Replies - 522 Views - Last Post: 20 April 2012 - 02:55 PM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Sending a List<T> to a website endpoint?

Posted 19 April 2012 - 05:05 PM

I'm trying to create a small utility program that parses .log files and constructs a single List<T> object full of relevant data.

This object must be passed to an ActionMethod on my MVC3 application (disregard the details of MVC3, it's not that relevant).

So from within my WPF application I need to somehow create a POST request with this List<T> object as the post data. Th examples I found on MSDN only deal with simple string values not complex objects.

http://msdn.microsof...y/debx8sh9.aspx

I'm sure I'm not the first one whose had to do this so any help would be very much appreciated. :)

For this particular instance, assume the endpoint is:

localhost:2020/upload/logs


Basically, I need to turn this:

List<T> parsedData;


Into this:

01827435012873409213409213j2jfj20d9j21d09jd210921j3d09j213d09j213d09j213d

This post has been edited by Sergio Tapia: 19 April 2012 - 05:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sending a List<T> to a website endpoint?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 05:23 AM

Are you coding both sides? If so, why don't you serialize it, POST the XML, then deserialize it on the server side?
Was This Post Helpful? 2
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 05:39 AM

Yes, both sides are under my direct 100% control.

On the client: A WPF application that generates a List<T> object.

On the server side: An MVC3 application waiting for that same List<T> object. (I copied the same model classes on both projects).

Thanks to your suggestion I already serialized my List<T> to XML using the [Serializable()] decorator. :)

Now:

Keyword: POST the XML

How do I do this? :lol:

This post has been edited by Sergio Tapia: 20 April 2012 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 05:59 AM

You have a link on how to POST a simple string. After serialization, the object is nothing but a simple string of XML. Post that string.

This is a small snippet of code that serializes a class into a MemoryStream, then reads that MemoryStream to get the XML that was created.

string xmlText = string.Empty;

using (Stream stream = new MemoryStream())
{
    XmlSerializer serializer = new XmlSerializer(typeof(Author));
    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.Default);
    writer.Formatting = Formatting.Indented;
    serializer.Serialize(writer, a);
    writer.Flush();

    stream.Position = 0;
    byte[] array = new byte[stream.Length];
    stream.Read(array, 0, array.Length);
    xmlText = Encoding.ASCII.GetString(array);
    writer.Close();
}


Was This Post Helpful? 1
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 06:15 AM

Ah of course! XML is just a string after all. (*Gets some first morning coffee* jajaja)

Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 02:17 PM

The documentation for this is terrible. Jesus!

Does anybody know how to convert an XML string back into a List<T>?

Here's what I've tried:


Here's how I did it:

string path = @"C:\Users\Sergio\Desktop\test3.log";
List<GameMatch> parsedMatches = LogParser.Parse(path);

XmlSerializer serializer = new XmlSerializer(typeof(List<GameMatch>));

StringWriter stringWriter = new StringWriter();
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

serializer.Serialize(xmlWriter, parsedMatches);

//At this point, I converted the List<T> to an XML string.
string xmlResult = stringWriter.ToString();
            
            
//Now, for testing, how to get this XML string back to a List<T>?
TextReader txtreader = new StringReader(xmlResult);
XmlReader reader = XmlReader.Create(txtreader);

// Use the Deserialize method to restore the object's state.
var matchesDeserialized = (List<GameMatch>)serializer.Deserialize(reader);


This post has been edited by Sergio Tapia: 20 April 2012 - 02:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Sending a List<T> to a website endpoint?

Posted 20 April 2012 - 02:55 PM

Just to help out, I have a couple of generic serialization methods that I use...

http://www.dreaminco...snippet6212.htm
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1