开发者

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

开发者 https://www.devze.com 2023-01-05 03:45 出处:网络
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.

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" />
    <Albu开发者_如何转开发m 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!


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.

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

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;
}


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>.

0

精彩评论

暂无评论...
验证码 换一张
取 消