Deserializing mixed content
See original GitHub issueHey,
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:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
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 👍
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