将XML反序列化为C#

时间:2014-08-14 08:12:55

标签: c# xml serialization

我有一些XML,我需要在c#中反序列化到我的对象中我已经提出了这个代码,但它没有工作它说我的PSTNPropsitionItem计数为零:

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

这是我的XML输出

<?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>

我执行此操作的代码将获取XML字符串并将其反序列化为上面的对象

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

任何人都可以看到我的代码可能出错吗?在反序列化xml之后,我的类对象中的Item计数总是返回0。

1 个答案:

答案 0 :(得分:3)

删除[XmlRoot("PropositionItem")]。只能有一个根。

您需要告诉解析器您有一个XML数组并指定它的名称。您还需要设置每个数组项的名称标记。

[XmlArray("PSTN")]
[XmlArrayItem("Item")]
public List<PSTNPropositionItem> Items { get; set; }