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

Time:09-17

I am facing the issue on getting the type of list object while serializing the xml input on WCF service. I am not getting the list value of Data class, it is giving count value as zero. I am new to WCF service. Could you please help me here?

below is the example:

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

And serializing xml data in service class

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

CodePudding user response:

XmlSerializer allows a list to be a sequence of elements with the same name,for example:

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

And then try this:

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

This is the test code :

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

The following xml is for your classes:

<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>
  • Related