Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML反序列化为C类#_C#_Xml_Serialization - Fatal编程技术网

C# 将XML反序列化为C类#

C# 将XML反序列化为C类#,c#,xml,serialization,C#,Xml,Serialization,我有一些XML需要反序列化到我的c#对象中,我已经想出了这段代码,但它不起作用,它说我的PSTNropsitionItem计数为零: [Serializable] [XmlRoot("Proposition")] public class Proposition { public Proposition() { } [XmlElement("CFWebResponse")] public CFWebResponse CFWebResponse { get; se

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

    [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字符串并将其反序列化到上面的对象中

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;
    }
从xmlstring(xmlResult,out PSTNProposition)反序列化;
公共静态bool反序列化fromXmlString(string xmlString,out T deserializedObject),其中T:class
{
反序列化对象=null;
尝试
{
如果(!string.IsNullOrEmpty(xmlString))
{
XmlSerializer serializer=新的XmlSerializer(typeof(T));
使用(StringReader StringReader=new StringReader(xmlString))
{
使用(XmlTextReader=new XmlTextReader(stringReader))
{
反序列化对象=序列化程序。反序列化(xmlReader)为T;
}
}
序列化程序=null;
}
if(反序列化对象!=null)
{
返回true;
}
}
捕获(例外情况除外)
{
//捕获异常等
}
返回false;
}
有人能看出我的代码哪里错了吗?在反序列化xml后,my class对象中的
项的计数始终返回0。

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

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

[XmlArray("PSTN")]
[XmlArrayItem("Item")]
public List<PSTNPropositionItem> Items { get; set; }
[XmlArray(“PSTN”)]
[XmlArrayItem(“项目”)]
公共列表项{get;set;}
[XmlArray("PSTN")]
[XmlArrayItem("Item")]
public List<PSTNPropositionItem> Items { get; set; }