C
C#15mo 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
Denis15mo ago
$code
MODiX
MODiX15mo 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
TheRanger15mo 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
joeystdioOP15mo 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
TheRanger15mo ago
probably a bug
joeystdio
joeystdioOP15mo ago
thanks for the tip
MODiX
MODiX15mo 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
TheRanger15mo ago
@joeystdio deserializes perfect for me
joeystdio
joeystdioOP15mo ago
thanks man! yes that works nicely
Rain
Rain15mo ago
anyone can help me as well
Rain
Rain15mo ago
Rain
Rain15mo ago
it becomes null after that line
SinFluxx
SinFluxx15mo 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 User15mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server