Deserializing XML into class C#

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.


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


  • How to check htmlElement object exists in DOM [duplicate]
  • Create XML DOM Element while keeping case sensitivity
  • Kubeadm init stops at image pulling
  • Is it ok to have a primary key relate to another primary key?
  • Javascript XML Data to Associative Array
  • Save array as xml
  • How do I assemble pieces of HTML into a DOMDocument?
  • Nodejs app with npm start script
  • “Message”:“Your request: '/_cluster/allocation/reroute' is not allowed.”}
  • perl windows IPv6
  • Error in simple python module
  • WARNING: Failed to connect to ip-xxx-xx-xx-xx
  • How to remove within a only
  • Define Datalist HeaderTemplate in c# from code behind
  • How to parse namespace XML in Chrome 24 and jQuery 1.8.2?
  • Trigger an actionscript function (in a flash object) from javascript
  • How to call firefox addon function with onclick in html
  • How to get div text also with it's input's values
  • Splash Screen will not display
  • Xamarin Android | Layout style
  • How to get current document uri in XSLT?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • Uncaught Error: Could not find module `ember-load-initializers`
  • output of program is not same as passed argument
  • Modifying destination and filename of gulp-svg-sprite
  • SSO with signing and signature validation doesn't work
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Is there a mandatory requirement to switch app.yaml?
  • How to include full .NET prerequisite for Wix Burn installer
  • Buffer size for converting unsigned long to string
  • python draw pie shapes with colour filled
  • costura.fody for a dll that references another dll
  • How to Embed XSL into XML
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize