本文介绍了C# .NET Web 服务并返回具有子对象列表的子对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个网络服务来传回(艺术家)对象的列表.在艺术家对象内部有一个(专辑)对象列表.在 Album 对象内有一个歌曲列表.

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.

我的问题是,如何使用 SOAP 传递它?

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

最好的方法是什么.

现在我得到的是

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

任何建议将不胜感激!

推荐答案

好消息是 .NET Web 服务将为您处理 XML.您所要做的就是将返回类型声明为 List 或类似的.Web 服务将负责将您的对象序列化为 XML.不清楚您是否正在滚动自己的 XML.

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.

您粘贴的 XML 看起来像是来自 WSDL.

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

在 Visual Studio 中运行您的项目,然后浏览到 Web 服务 .asmx 页面.你会发现类似的东西.

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

要使用 HTTP POST 协议测试操作,请单击调用"按钮.

单击该按钮以运行您的方法.

Click that button to have your method run.

如果您自己的 WebMethod 没有按预期工作,也许可以试试这个简单的测试 WebMethod:结果将是这个 XML 文档.

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

这篇关于C# .NET Web 服务并返回具有子对象列表的子对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:28