简体   繁体   English

如何序列化从C#ASP.NET 3.5中的Web服务返回的对象列表?

[英]How to serialize a List of Objects returned from a webservice in C# ASP.NET 3.5?

Here is the XML that I'd like the webservice to deliver: 这是我希望web服务提供的XML:

<business>
  <locations>
    <location>location 1</location>
    <location>location 2</location>
  </locations>
</business>

However, instead the following is being returned: 但是,而是返回以下内容:

<business>
  <locations>
    <location>
      <name>location 1</name>
    </location>
    <location>
       <name>location 2</name>
    </location>
  </locations>
</business>

Here is the code used: 这是使用的代码:

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public Business GetBusiness()
    {
        var business = new Business();
        business.Locations = new List<Location>();
        business.Locations.Add(new Location { Name = "location 1" });
        business.Locations.Add(new Location { Name = "location 2" });
        return business;
    }

    [XmlType(TypeName = "business")]
    public class Business
    {
        [XmlArray(ElementName = "locations")]
        [XmlArrayItem(ElementName = "location")]
        public List<Location> Locations { get; set; }
    }

    [XmlType(TypeName = "location")]
    public class Location
    {
        [XmlElement(ElementName = "name")]
        public string Name { get; set; }
    }

How does one get the location string include the location tag instead of having a name tag? 如何获取位置字符串包含位置标记而不是名称标记?

TIA, George TIA,乔治

You need to use the XmlTextAttribute on the Name member to treat it as the text of an XML element: 您需要使用Name成员上的XmlTextAttribute将其视为XML元素的文本:

[XmlType(TypeName = "location")]
public class Location
{
    [XmlText()]
    public string Name { get; set; }
}

Why not just use a list of strings for locations instead of a list of location objects? 为什么不只使用位置字符串列表而不是位置对象列表?

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public Business GetBusiness()
    {
        var business = new Business();
        business.Locations = new List<string>();
        business.Locations.Add("location 1");
        business.Locations.Add("location 2");
        return business;
    }

    [XmlType(TypeName = "business")]
    public class Business
    {
        [XmlArray(ElementName = "locations")]
        [XmlArrayItem(ElementName = "location")]
        public List<string> Locations { get; set; }
    }

    //[XmlType(TypeName = "location")]
    //public class Location
    //{
    //    [XmlElement(ElementName = "name")]
    //    public string Name { get; set; }
    //}

That results in the XML you're looking for. 这导致您正在寻找的XML。

You can control the serialization and deserialization process using a number of methods specified in MSDN: http://msdn.microsoft.com/en-us/library/ty01x675%28v=vs.80%29.aspx 您可以使用MSDN中指定的许多方法来控制序列化和反序列化过程: http//msdn.microsoft.com/en-us/library/ty01x675%28v=vs.80%29.aspx

In your case you are serializing a list of locations, using the ElementName "location". 在您的情况下,您使用ElementName“location”序列化位置列表。 If you were serializing a list of strings public List<string> Locations {get; set; } 如果您正在序列化字符串public List<string> Locations {get; set; } public List<string> Locations {get; set; } public List<string> Locations {get; set; } you would get the expected result. public List<string> Locations {get; set; }你会得到预期的结果。 The Location object you use places an extra level in the hiearchy, thus is serialized on it's own with it's own properties (like name). 您使用的Location对象在hiearchy中放置了一个额外的级别,因此它自己使用它自己的属性(如名称)进行序列化。

If you implement the ISerializable interface on the Locations class, you will be in full control of the generated XML output, and can simply pass back the contents of your 'Name' property. 如果在Locations类上实现ISerializable接口,您将完全控制生成的XML输出,并且可以简单地传回“Name”属性的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM