Question

I have classes generated via xsd.exe (xsd.exe someschema.xsd /classes). One of the nodes is declared as an element:

<xs:element name="containsxmlelementsbeneath"/>

As the (ficticious) name implies, it looks like this:

<containsxmlelementsbeneath>
  <somemore>
    ...
  </somemore>
</containsxmlelementsbeneath>

When it is deserialized, I see in the debugger that it is of type

System.Xml.XmlNode[]

I can force it in the Immediate Window

?((System.Xml.XmlNode[])elem.containsxmlelementsbeneath)[0].InnerXml

I got no IntelliSense, which made sense when I tried the snippet in my code - the class seems to have been removed from the WinRT profile - which is nice if all you need is Windows.Data.Xml.Dom.IXmlNode - but not in this case.

How can I go and get the string representation of that element? Is there a way to "fix" the xsd.exe-generated output so it uses Windows.Data.Xml.Dom for Serialization? (doesn't look like it to me)

Did I hit a fringe case they didn't think about?

Update - tried the following (I know, an abuse of dynamic):

dynamic x = elem.containsxmlelementsbeneath;
string s = x[0].InnerXml;

This yields "The API 'System.Xml.XmlNode[]' cannot be used on the current platform."

Was it helpful?

Solution

I had a (longer) chat with another dev - after some trial and error we came up with a solution:

<xs:element ref="containsxmlelementsbeneath"/>

<xs:element name="containsxmlelementsbeneath">
</xs:element>

This creates an empty class for us (via xsd.exe)

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class containsxmlelementsbeneath
{
}

This has to be modified like this:

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class containsxmlelementsbeneath : IXmlSerializable
{
    [XmlIgnore]
    public string Text { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new System.NotImplementedException();
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        Text = reader.ReadInnerXml();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        throw new System.NotImplementedException();
    }
}

Note that all attributes except XmlRoot have to be removed, otherwise you get Reflection Exceptions (Only XmlRoot attribute may be specified for the type containsxmlelementsbeneath. Please use XmlSchemaProviderAttribute to specify schema type.)

End Result: this node with all its subnodes as a plain old string. No non-accesible XmlNode or XmlElement any more...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top