question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing mixed content

See original GitHub issue

Hey,

I’m trying to convert some XDocument code to deserialize some mixed XML content to use ExtendedXmlSerializer to greatly simplify the code. I started with the plain old XmlSerializer, but quickly ran into issues where I had to serialize an attribute that could either be a decimal or hexadecimal string which was easily solved with a custom converter but now I’ve run into the next issue which I can’t quite figure out how to solve.

The relevant section of XML looks as follows:

<StringFormat>Example <Ref field="Foo" /> bit of mixed <Ref field="Bar" /> xml content <Ref field="Baz" />ms</StringFormat>

I’m trying to get this into some data structures along these lines:

    public class MessageStringRef
    {
        [XmlAttribute("field")]
        public string Reference { get; set; }
    }

    public class MessageStringFormat
    {
        [XmlText(typeof(string))]
        [XmlElement("Ref", typeof(MessageStringRef))]
        public List<object> Parts { get; set; }
    }

So far everything I’ve tried with attributes results in Unknown/invalid member encountered: 'field'. I have no idea if ExtendedXmlSerializer even supports mixed content, so I tried registering a custom serializer for the StringFormat element. However, if I try to access parameter.Content() inside the serializer all I get is the text up until the first XML element, in the case of the example that’s "Example " which makes it impossible to do any custom parsing myself. (In the worst case, if I could get the entire body of the <StringFormat> node I can just fall back to the old XDocument parsing)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Goz3rrcommented, Sep 16, 2020

Currently the workaround is good enough for me and I just added a little extra processing in the serializer to deal with the whitespace, thanks for the suggestion and looking into a potential fix 👍

1reaction
Goz3rrcommented, Sep 11, 2020

I tried to make a small repro but can’t seem to trigger the same issue, I’ll get back to you on Monday when I can look at my code that doesn’t work

Read more comments on GitHub >

github_iconTop Results From Across the Web

XmlSerializer Mixed Content Deserialization [duplicate]
How do I setup my class to deserialize the following mixed content xml? <employee> <name>John Doe</name> <remark>He is a <match>tall</match> ...
Read more >
CA2305: Do not use insecure deserializer LosFormatter
Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected ...
Read more >
C# Deserialize text in XML mixed content as custom object?
Coding example for the question C# Deserialize text in XML mixed content as custom object?-C#. ... Why deserialize XML into Object return null...
Read more >
Insecure Deserialization with JSON .NET | by Nairuz Abulhul
Insecure deserialization is passing manipulated serialized objects that can be interpreted by the application leading to its control. The impact ...
Read more >
quick_xml::de - Rust
Mixed text / CDATA content represents one logical string, "textcdatatext" in that case. You can use any type that can be deserialized from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found