0

I have the following code which is supposed to deserialize an XML string into a Class that has most of the items in the XML file, it may not have all them which is fine they should just assume NULL.

When i run the following code using the XML below it leaves each value as NULL.

Any pointers for where i'm going wrong

Thanks

ServiceResponse ReturnVal = new ServiceResponse();

try
{
    XmlSerializer serializer = new XmlSerializer(typeof(ServiceResponse));

    StringReader sr = new StringReader(XMLResponse);

    NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr);

    ReturnVal = (ServiceResponse)serializer.Deserialize(XMLWithoutNamespace);

}
catch (Exception ex)
{
    throw ex;
}


[XmlRoot("ServiceResponse")]
public class ServiceResponse
{
    public string RequestType { get; set; }

    public string ApplicationSender { get; set; }

    public string WorkstationID { get; set; }

    public string POPID { get; set; }

    public string RequestID { get; set; }

    public string ReferenceNumber { get; set; }

    public string ProtocolVersion { get; set; }

    public string DeviceType { get; set; }

    public string SWChecksum { get; set; }

    public string CommunicationProtocOl { get; set; }

    public string Model { get; set; }

    public string ApplicationSoftwareVersion { get; set; }

    public string Manufacturer_Id { get; set; }

    public string OverallResult { get; set; }

}

public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
    public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }

    public override string NamespaceURI
    {
        get { return ""; }
    }
}   

XMLResponse will be equal too the following:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><ServiceResponse RequestType="Login" ApplicationSender="QWERTY" WorkstationID="1" RequestID="1254" ProtocolVersion="12" DeviceType="1" SWChecksum="1" CommunicationProtocol="11" Model="1" ApplicatioSoftwareVersion="010" Manufacturer_Id="0" OverallResult="Success" xmlns="http://www.nrf-arts.org/IXRetail/namespace" xmlns:IFSF="http://www.ifsf.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nrf-arts.org/IXRetail/namespace C:\Schema\ServiceResponse.xsd" />
2
  • You need to identify your properties as attributes otherwise it will try to read them as sub-nodes. See msdn.microsoft.com/en-us/library/… Commented Jul 14, 2015 at 15:33
  • Thanks Ron, Just figured that out now Commented Jul 14, 2015 at 15:46

2 Answers 2

1

All your properties are attributes, and the XmlSerializer will interpret them as elements unless you tell it otherwise.

Add [XmlAttribute] attributes to each of your properties and it will work.

As an aside, throw ex will dump the stack trace and that isn't usually what you want - see this question for further detail. If you're not going to do anything with the exception, removing try/catch entirely would be better.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Charles, Just saw your answer now, I had just worked it out and was posting the update. Thanks for the advice RE: throw ex
0

Worked it out, was missing the decoration in the class to Map in too. [XmlAttribute()] public string RequestType { get; set; }

    [XmlAttribute()]
    public string ApplicationSender { get; set; }

    [XmlAttribute()]
    public string WorkstationID { get; set; }

    [XmlAttribute()]
    public string POPID { get; set; }

    [XmlAttribute()]
    public string RequestID { get; set; }

    [XmlAttribute()]
    public string ReferenceNumber { get; set; }

    [XmlAttribute()]
    public string ProtocolVersion { get; set; }

    [XmlAttribute()]
    public string DeviceType { get; set; }

    [XmlAttribute()]
    public string SWChecksum { get; set; }

    [XmlAttribute()]
    public string CommunicationProtocOl { get; set; }

    [XmlAttribute()]
    public string Model { get; set; }

    [XmlAttribute()]
    public string ApplicationSoftwareVersion { get; set; }

    [XmlAttribute()]
    public string Manufacturer_Id { get; set; }

    [XmlAttribute()]
    public string OverallResult { get; set; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.