将 XML 反序列化为类 C# [英] Deserializing XML into class C#

查看:33
本文介绍了将 XML 反序列化为类 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 XML 需要在 c# 中反序列化到我的对象中我已经想出了这段代码但它不起作用它说我的 PSTNPropsitionItem 计数为零:

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

这是我的 XML 输出

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>

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

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

谁能看到我的代码哪里出错了?xml 反序列化后,我的类对象中的 Item 计数始终返回 0.

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.

推荐答案

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

Remove [XmlRoot("PropositionItem")]. There can only be one root.

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

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

这篇关于将 XML 反序列化为类 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆