C
C#2y ago
joeystdio

✅ XML deserialization problem

Can anyone please help me sleep again.... 🙂 When deserializing the following XML to the respective object it doesn't deserialize anything after the empty nodes (eg: ProcessingLog). note: if I remove the empty nodes OR move the ProcessingLog node above the empty nodes it does get deserialized. Any tips would be greatly appreciated
15 Replies
Denis
Denis2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
TheRanger
TheRanger2y ago
hmm sounds like a weird bug you can probably need to replace nodes like <MessageNumberCollection /> with <MessageNumberCollection></MessageNumberCollection> using string.Replace using regex may help
joeystdio
joeystdioOP2y ago
ah thanks, I was already wondering how to do that Hmmm okay... sounds like a dirty workaround 😄 That indeed works... but why ... <Data /> should be the same as a separate open / closing tag
TheRanger
TheRanger2y ago
probably a bug
joeystdio
joeystdioOP2y ago
thanks for the tip
MODiX
MODiX2y ago
TheRanger
REPL Result: Success
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<UniversalResponse version=""1.1"" xmlns=""http://www.cargowise.com/Schemas/Universal/2011/11"">
<Status>ERR</Status>
<Data />
<MessageNumberCollection />
<ProcessingLog>Unrecognized root element or malformed XML. Examples of supported root elements are: UniversalShipmentRequest, Native, etc.</ProcessingLog>
</UniversalResponse>";
string pattern = @"<(\w+)(\s+|)\/>";
string replacement = @"<$1></$1>";
Regex regex = new Regex(pattern);
string result = regex.Replace(xml, replacement);
Console.WriteLine(result);
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<UniversalResponse version=""1.1"" xmlns=""http://www.cargowise.com/Schemas/Universal/2011/11"">
<Status>ERR</Status>
<Data />
<MessageNumberCollection />
<ProcessingLog>Unrecognized root element or malformed XML. Examples of supported root elements are: UniversalShipmentRequest, Native, etc.</ProcessingLog>
</UniversalResponse>";
string pattern = @"<(\w+)(\s+|)\/>";
string replacement = @"<$1></$1>";
Regex regex = new Regex(pattern);
string result = regex.Replace(xml, replacement);
Console.WriteLine(result);
Console Output
<?xml version="1.0" encoding="utf-8"?>
<UniversalResponse version="1.1" xmlns="http://www.cargowise.com/Schemas/Universal/2011/11">
<Status>ERR</Status>
<Data></Data>
<MessageNumberCollection></MessageNumberCollection>
<ProcessingLog>Unrecognized root element or malformed XML. Examples of supported root elements are: UniversalShipmentRequest, Native, etc.</ProcessingLog>
</UniversalResponse>
<?xml version="1.0" encoding="utf-8"?>
<UniversalResponse version="1.1" xmlns="http://www.cargowise.com/Schemas/Universal/2011/11">
<Status>ERR</Status>
<Data></Data>
<MessageNumberCollection></MessageNumberCollection>
<ProcessingLog>Unrecognized root element or malformed XML. Examples of supported root elements are: UniversalShipmentRequest, Native, etc.</ProcessingLog>
</UniversalResponse>
Compile: 614.765ms | Execution: 72.155ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
@joeystdio deserializes perfect for me
joeystdio
joeystdioOP2y ago
thanks man! yes that works nicely
Rain
Rain2y ago
anyone can help me as well
Rain
Rain2y ago
Rain
Rain2y ago
it becomes null after that line
SinFluxx
SinFluxx2y ago
@Rain you should start your own thread And provide further detail at first glance you're not awaiting the async method call
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered

Did you find this page helpful?