1

这是我收到的 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 有什么问题吗?

4

1 回答 1

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 元素的事实。

于 2012-05-06T15:29:53.943 回答