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

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

Basically, I create a large parent music child tree.

My question is how to pass this using SOAP?

What is the best way to use.

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 into albums, but it shows two albums that I saved.

Any suggestions would be appreciated!

+4
source share
2 answers

The good news is that the .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 in XML for you. It's not clear if you roll your own XML.

The XML you enclose looks as if it came from WSDL.

Run the project in Visual Studio and go to the .asmx web page. You will find something like this.

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

Click this button to launch your method.

Perhaps try this simple WebMethod test if your own does not work as you expect: The result will be this XML document .

  [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; } 
+7
source

Something like that:

 [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 serialized. Modify it to use properties instead of public fields, etc.

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

+2
source

Source: https://habr.com/ru/post/1314396/


All Articles