4

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!

2
  • What kind of List are you using? Is it the same for Artist, Albums and Songs Commented Jul 1, 2010 at 4:53
  • I am ultimately return List<Artists> from the web service call. Artist Contains List<albums> and Albums Contain List<Songs>. I tried to do generics List<T> but didn't work. Commented Jul 1, 2010 at 4:59

2 Answers 2

8

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am definitley not trying to role my own XML. I copied that code from the .asmx page
Even I have a similar web service, I would like to know how to consume it in windows form application. How do I do that?
2

Something like this :

[DataContract]
class Artist
{
   .....
   [DataMember]
   public List<Album> Albums;
   .....
}

[DataContract]
class Album
{
   .....
   int ID;
   .....
   [DataMember]
   public List<Song> Songs;
   .....
}

[DataContract]
class Song
{
   .....
   int ID;
   .....
   [DataMember]
   public string Title;
   .....
}

NOTE : This is just to show how to make your objects serialize. Modify it to use properties instead of public fields, etc...

You need just to implement WCF service and use List<Artist>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.