0

I'm using a 3rd party API to pull some data. The wrapper is held in a list array type. I've managed to pull the data but now I want to display it in my console window in .NET. I thought just using this would be fine:

Console.WriteLine(Obj.GetData(datapull));

However, the output is as follows:

System.Collections.Generic.List`1[CompanyProduct.API.DataRequestItem]

I think I can use a for each loop but I don't know how to do that and it seems overly complicated because I only have a single value to pull! But according to the API doc I have to use the List array for the wrapper.

I also tried to convert the list to a string, like this:

string dataCombined = string.Join( ",", datapull);

This too did not work, and printed the following:

CompanyProduct.API.<name of class for data request>

Can anyone help me or at least point me in the right direction. I thought what I was trying to do was easy!!!

Thanks for any help.

Jay

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Jay Parken
  • 173
  • 1
  • 2
  • 8

1 Answers1

0

It depends on what information of the object you want to print out to the console and if the class has implemented/override the ToString() method. When the method is not implemented (the normal case), only the type name gets written to Console. Console.WriteLine uses the ToString() to print out the object.

Since you are using a 3rd party API and you cannot change the implemention of the ToString() method you need to use some kind of ObjectDumper, that prints out an object, with all its properties and property values.

ObjectDumper use a specify format, such as XML, JSON or custom to create a string representation of your object. Questions and answert to ObjectDumper in c# you can find here for example. A method that prints your object to xml looks like (taken from the link):

private static string ObjectToXml(object objectToDump)
{
 string xmlRepresentation;

 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(objectToDump.GetType());
 using (System.IO.StringWriter sw = new System.IO.StringWriter())
 {
    try
    {
       xs.Serialize(sw, objectToDump);
       xmlRepresentation = sw.ToString();
    }
    catch (Exception ex)
    {
       xmlRepresentation = ex.ToString();
    }
 }

 return xmlRepresentation;
 }
Community
  • 1
  • 1
Jehof
  • 34,674
  • 10
  • 123
  • 155