958

I have some XML which i need to deserialize into my object in c# I have come up with this code but its not working it says my PSTNPropsitionItem count is zero:
[Serializable]
[XmlRoot("Proposition")]
public class Proposition
{
public Proposition() { }
[XmlElement("CFWebResponse")]
public CFWebResponse CFWebResponse { get; set; }
[XmlElement("PropositionItem")]
public List<PSTNPropositionItem> Items { get; set; }
}
[Serializable]
[XmlRoot("PropositionItem")]
public class PropositionItem
{
public PropositionItem() { }
[XmlElement("PackageCode")]
public string PackageCode { get; set; }
[XmlElement("ProductCode")]
public string ProductCode { get; set; }
[XmlElement("UnitPrice")]
public decimal UnitPrice { get; set; }
[XmlElement("SetupCost")]
public decimal SetupCost { get; set; }
[XmlElement("PricePlanCode")]
public decimal PricePlanCode { get; set; }
[XmlElement("ComponentType")]
public decimal ComponentType { get; set; }
[XmlElement("NodeName")]
public decimal NodeName { get; set; }
[XmlElement("MaxQty")]
public decimal MaxQty { get; set; }
}
And here is my XML output
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Proposition xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<PSTN>
<Item>
<PackageCode>2201</PackageCode>
<ProductCode>E/CS/WLR_BUS</ProductCode>
<UnitPrice>11.5000</UnitPrice>
<SetupPrice>0.0000</SetupPrice>
<PricePlanCode>MA</PricePlanCode>
<ComponentType xsi:nil=\"true\"/>
<NodeName>PSTN</NodeName>
<MaxQty xsi:nil=\"true\"/>
</Item>
<Item>
<PackageCode>2201</PackageCode>
<ProductCode>E/CS/TM2</ProductCode>
<UnitPrice>1.0000</UnitPrice>
<SetupPrice>0.0000</SetupPrice>
<PricePlanCode>MA</PricePlanCode>
<ComponentType xsi:nil=\"true\"/>
<NodeName>CallPackage</NodeName>
<MaxQty xsi:nil=\"true\"/>
</Item>
</PSTN>
<CFWebResponse>
<Success>true</Success>
<Code>code</Code>
</CFWebResponse>
</Proposition>
My code to do this takes the XML string and deserializes it into the object above
DeserializeFromXmlString(xmlResult, out PSTNProposition);
public static bool DeserializeFromXmlString<T>(string xmlString, out T deserializedObject) where T : class
{
deserializedObject = null;
try
{
if (!string.IsNullOrEmpty(xmlString))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader stringReader = new StringReader(xmlString))
{
using (XmlTextReader xmlReader = new XmlTextReader(stringReader))
{
deserializedObject = serializer.Deserialize(xmlReader) as T;
}
}
serializer = null;
}
if (deserializedObject != null)
{
return true;
}
}
catch (Exception ex)
{
//catch exception etc
}
return false;
}
Can anyone see where my code may be wrong? The Item
count in my class object always returns 0 after the xml has be deserialized.
Answer1:
Remove [XmlRoot("PropositionItem")]
. There can only be one root.
You need to tell the parser that you have an XML array and specify it's name. You also need to set the name tag of each array item.
[XmlArray("PSTN")]
[XmlArrayItem("Item")]
public List<PSTNPropositionItem> Items { get; set; }