C
C#3y ago
Iceman228

XML Deserialization Null Exception

I have an XML element which usually looks like this
<LoginName><![CDATA[Name]]></LoginName>
<LoginName><![CDATA[Name]]></LoginName>
It is possible thought that someone deleted the whole cdata part and if that happened, when trying to deserialize it, I get a NullReferenceException.
public static XmlCDataSection ToCData(this string str)
{
return new XmlDocument().CreateCDataSection(str);
}

[XmlIgnore] public string LoginName { get; set; }
[XmlElement("LoginName")]
public XmlCDataSection CDataLoginName
{
get => LoginName.ToCData();
set => LoginName = value.Value;
}
public static XmlCDataSection ToCData(this string str)
{
return new XmlDocument().CreateCDataSection(str);
}

[XmlIgnore] public string LoginName { get; set; }
[XmlElement("LoginName")]
public XmlCDataSection CDataLoginName
{
get => LoginName.ToCData();
set => LoginName = value.Value;
}
How can I prevent this exception?
2 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Iceman228
Iceman228OP3y ago
Is there a reason not to and would that change the behavior ?
set => LoginName = value?.Value;
set => LoginName = value?.Value;
I did this and it is working now.
Want results from more Discord servers?
Add your server