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