【问题标题】:Deserializing XML into class C#将 XML 反序列化为 C# 类
【发布时间】:2015-01-27 21:51:30
【问题描述】:

我有一些 XML 需要在 c# 中反序列化到我的对象中

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

谁能看到我的代码可能出错的地方?我的类对象中的 Item 计数在 xml 被反序列化后总是返回 0。

【问题讨论】:

    标签: c# xml serialization


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多