序列化时未在 wcf 服务中获取 xml 输入值以列出 class

not getting xml input value to list class in wcf service while serializing

我在序列化 WCF 服务上的 xml 输入时遇到获取列表对象类型的问题。我没有得到数据 class 的列表值,它给出的计数值为零。我是 WCF 服务的新手。你能帮我一下吗?

示例如下:

[DataContract]
public class Item
{ 
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public List<Data> data { get; set; }
}

[DataContract]
public class Data
{
    [DataMember]
    public int dataId { get; set; }
    [DataMember]
    public int dataName { get; set; }
    [DataMember]
    public int dataVolume { get; set; }
}

并在服务中序列化 xml 数据 class

public Stream Conversion(Stream request)
{
    XmlSerializer serializer1 = new XmlSerializer(typeof(Item));
    Item item = (Item)serializer1.Deserialize(request);
}

XmlSerializer 允许列表是同名元素的序列,例如:

[XmlRoot("Item", Namespace="")]
public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    [XmlElement("Data")]
    public List<Data> Data { get; set; }
}

public class Data
{
    [DataMember]
    public int dataId { get; set; }
    [DataMember]
    public string dataName { get; set; }
    [DataMember]
    public int dataVolume { get; set; }
}

然后试试这个:

public static class XmlSerializationHelper
{
    public static string GetXml<T>(T obj, XmlSerializer serializer, bool omitStandardNamespaces)
    {
        using (var textWriter = new StringWriter())
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;        // For cosmetic purposes.
            settings.IndentChars = "    "; // For cosmetic purposes.
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                if (omitStandardNamespaces)
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
                    serializer.Serialize(xmlWriter, obj, ns);
                }
                else
                {
                    serializer.Serialize(xmlWriter, obj);
                }
            }
            return textWriter.ToString();
        }
    }

    public static string GetXml<T>(this T obj, bool omitNamespace)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        return GetXml(obj, serializer, omitNamespace);
    }

    public static string GetXml<T>(this T obj)
    {
        return GetXml(obj, false);
    }
}

这是测试代码:

var item = new Item { Id= 1, Name = "The Car", Data = new[] { new Data { dataId = 1, dataName = "Vovol",dataVolume = 135 }, new Data { dataId = 2, dataName = "Cadillac",dataVolume = 136 } }.ToList() };
var xml = item.GetXml();

Debug.WriteLine(xml);

以下xml是给你的类:

<Item>
    <Id>1</Id>
    <Name>The Car</Name>
    <Data>
        <dataId>1</dataId>
        <dataName>Vovol</dataName>
        <dataVolume>135</dataVolume>
    </Data>
    <Data>
        <dataId>2</dataId>
        <dataName>Cadillac</dataName>
        <dataVolume>136</dataVolume>
    </Data>
</Item>