簡體   English   中英

將XML反序列化為類C#

[英]Deserializing XML into class C#

我有一些需要在C#中反序列化到我的對象中的XML,我想出了這段代碼,但是它無法正常工作,它說我的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。

刪除[XmlRoot("PropositionItem")] 只能有一個根。

您需要告訴解析器您有一個XML數組並指定其名稱。 您還需要設置每個數組項的名稱標簽。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM