我正在将以下XML反序列化为C#对象
<?xml version="1.0" encoding="utf-8"?>
<Message>
<Request>
<FromInfo>
<Provider>
<ProviderName><![CDATA[Name]]></ProviderName>
</Provider>
</FromInfo>
<ToInfo>
<Provider>
<ProviderID><![CDATA[123]]></ProviderID>
<ID><![CDATA[1]]></ID>
</Provider>
</ToInfo>
</Request>
<RS>
<Add>
<AccountInfoRecord>
<UserInfo>
<FirstName><![CDATA[Some]]></FirstName>
<LastName><![CDATA[Name]]></LastName>
<DayPhone><![CDATA[0123456]]></DayPhone>
<Email><![CDATA[SampleEmail]]></Email>
<UserAddress>
<Address1><![CDATA[Address]]></Address1>
<Address2><![CDATA[Something]]></Address2>
<City><![CDATA[Oslo]]></City>
<State />
<PostalCode><![CDATA[1330]]></PostalCode>
<Country><![CDATA[some]]></Country>
</UserAddress>
</UserInfo>
<YourUserKey />
</AccountInfoRecord>
</Add>
</RS>
</Message>
我在<State />
中遇到错误
但不在<YourUserKey />
和<State></State>
以下是错误
Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Xml.XmlCDataSection'.
这是模型类
[XmlElement(ElementName = "State")]
public XmlCDataSection StateCData
{
get
{
return !string.IsNullOrEmpty(State)
? new XmlDocument().CreateCDataSection(State) : null;
}
set
{
State = value?.Value;
}
}
我也经历了多个线程,但找不到具体答案。