序列化错误,我似乎无法解决

时间:2012-05-06 14:57:27

标签: c#-4.0

这是我收到的xml。

<ussd PDU="CTRL" MSISDN="*" STRING="2012/05/06 04:27:06 PM" TID="0" REQID="0"  ENCODING="ASCII" TARIFF="*" STATUS="0"><cookie/></ussd>

这是我的班级结构:

[Serializable]
[XmlRoot(ElementName = "ussd", Namespace = "")]
public class ussd
{
    [XmlAttribute(AttributeName = "PDU")]
    public string PDU = string.Empty;

    [XmlAttribute(AttributeName = "MSISDN")]
    public string MSISDN = string.Empty;

    [XmlAttribute(AttributeName = "STRING")]
    public string STRING = string.Empty;

    [XmlAttribute(AttributeName = "TID")]
    public string TID = string.Empty;

    [XmlAttribute(AttributeName = "REQID")]
    public string REQID = string.Empty;

    [XmlAttribute(AttributeName = "ENCODING")]
    public string ENCODING = string.Empty;

    [XmlAttribute(AttributeName = "TARIFF")]
    public string TARIFF = string.Empty;

    [XmlAttribute(AttributeName = "STATUS")]
    public string STATUS = string.Empty;
}

为什么不能将这个xml序列化为这个类?任何人都可以看到我看不到的xml有问题吗?

1 个答案:

答案 0 :(得分:0)

  

为什么不能将这个xml序列化到这个类中?

它可以:

string xml = @"<ussd PDU=""CTRL"" MSISDN=""*"" STRING=""2012/05/06 04:27:06 PM"" TID=""0"" REQID=""0""  ENCODING=""ASCII"" TARIFF=""*"" STATUS=""0""><cookie/></ussd>";

using(StringReader stringReader = new StringReader(xml))
using (XmlTextReader xmlReader = new XmlTextReader(stringReader))
{

    XmlSerializer serializer = new XmlSerializer(typeof(ussd));
    ussd myobject = (ussd)serializer.Deserialize(xmlReader);

    Console.WriteLine(myobject.ENCODING);
}
  

任何人都可以看到我看不到的xml有问题吗?

只是你没有反序列化cookie元素。