I have a schema from a third party that I've generated c# objects from using Xsd2Code (other options like xsd.exe, xmlspy etc either crashed or spewed 40mb of code that required their library to work)
Anyway, here's an example of a problem element from the schema:
<xsd:schema xmlns:ns1="something" xmlns:ns2="somethinelse" targetNamespace="something">
<xsd:complexType name="someType">
<xsd:sequence>
<xsd:element ref="element1" />
<xsd:element ref="ns2:element2" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The generated wrapper class looks like this:
[XmlType(Namespace="something")]
[XmlRoot("someType", Namespace="something", IsNullable=false)]
public partial class SomeType {
public string Element1 { get; set; }
[XmlElement(Namespace="somethinelse")]
public string Element2 { get; set; }
}
Example xml using said schema:
<someType>
<element1>SomeValue</element1>
<ns2:element2>SomeValue2</element2>
</someType>
(Any errors are my typing the example. the schema is valid and un-changeable)
And now for the problem. when I try to deserialize the xml like so:
XmlSerializer ser = new XmlSerializer(typeof(SomeType));
XmlReader reader = XmlReader.Create(new StringReader(xmlString))
SomeType obj = (SomeType)ser.Deserialize(reader)
The generated objects serialize correctly, adding the "ns2" to elements that need it. However, when deserializing, element1 gets set and element2 is left null.
The sample data we were provided with had a typeo in the namespace which was causing it to deserialize wrong. Go figure.
精彩评论