Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET web service and returning list of objects that have children with a list of child objects

I am building a web service to pass back a list of (artist) objects. Inside of the artist object there is a list of (album) objects. Inside the Album object there is a list of songs.

SO basically I am building a big parent child tree of music.

My question is, how to I pass this using SOAP?

What is the best method to use.

Right now All I get is

<Artist>
    <Name>string</Name>
    <Albums>
        <AlbumName>string</AlbumName>
        <Album xsi:nil="true" />
    <Album xsi:nil="true" />
    </Albums>
    <Albums>
        <AlbumName>string</AlbumName>
        <Album xsi:nil="true" />
        <Album xsi:nil="true" />
    </Albums>
</Artist>

It breaks down at albums but it shows two albums which I have stored.

Any Suggestions would be appreciated!

like image 216
sdmiller Avatar asked Dec 29 '22 12:12

sdmiller


1 Answers

The good news is that a .NET web service will take care of the XML for you. All you have to do is declare the return type as List<Artist> or similar. The web service will take care of serializing your objects into XML for you. It's not clear if you were rolling your own XML.

The XML you've pasted looks like it came from the WSDL.

Run your project in Visual Studio, and browse to the web service .asmx page. You'll find something like this.

To test the operation using the HTTP POST protocol, click the 'Invoke' button. alt text

Click that button to have your method run.

Perhaps try this simple test WebMethod if your own isn't working as you expect: The result will be this XML doc.

  [WebMethod]
  public List<Artist> ListAllArtists()
  {
      List<Artist> all = new List<Artist>();
      Album one = new Album { Name = "hi", SongNames = new List<string> { "foo", "bar", "baz" } };
      Album two = new Album { Name = "salut", SongNames = new List<string> { "green", "orange", "red" } };
      Album three = new Album { Name = "hey", SongNames = new List<string> { "brown", "pink", "blue" } };
      Album four = new Album { Name = "hello", SongNames = new List<string> { "apple", "orange", "pear" } };

      all.Add(new Artist { Albums = new List<Album> { one }, Name = "Mr Guy" });
      all.Add(new Artist { Albums = new List<Album> { two }, Name = "Mr Buddy" });
      all.Add(new Artist { Albums = new List<Album> { three, four }, Name = "Mr Friend" });

      return all;        
  }

public class Artist
{
    public List<Album> Albums;
    public string Name;
}

public class Album
{
    public string Name;
    public List<string> SongNames;
}
like image 178
p.campbell Avatar answered Dec 31 '22 15:12

p.campbell