1

I have some xml that I need to deserialize:

<MysteryXml niceAttribute="I know">
    <UnknownNode attr1="someValue" attr2="anotherValue"> <- this nodes attributes and children are unknows
        <Name>
            <Value />
            <AnotherValue />
        </Name>
        <CreatedBy>Unit Test</CreatedBy>
    </UnknownNode>
    <KnownNode nice="anotherAttr" />
</MysteryXml>

I have a C# class to deserialize the xml to

    [Serializable()]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    [XmlRoot(ElementName = "MysteryXml", IsNullable = false)]
    public class MysteryXml
    {
        [XmlElement] 
        // Here I have a node[] but I would like an XElement
        // If I set the type to XElement I only get the child node
        public object? UnknownNode { get; set; }

        [XmlElement]
        public KnownNode KnownNode { get; set; }

        [XmlAttribute]
        public string niceAttribute {get;set;}
    }

Is it possible to get the full xml as a string or XElement for UnknownNode using XmlSerializer?

Thanks

  • 1
    This sounds a lot like this? https://stackoverflow.com/questions/34787284/xmlserializer-keep-unknown-elements/34788431 (short version: `[XmlAnyElement]`) – Marc Gravell May 21 '21 at 12:40

0 Answers0