I have a bunch of C# classes, which are auto generated from an XSD. Then I generate XML files based on those C# classes. Nothing existing so far.
The problem:
The generated XML files are going through validation and the validation requires an extra attribute to all XML tags with xsi:nil="true"
. Basically the tags should look like : <testTag.01 xsi:nil="true" NV="123123" />
, but I can't achieve that in C#. My code is:
if (myObject.TestTag.HasValue) { t.testTag01 = new testTag01(); t.testTag01.Value = myObject.TestTag.Value; } //else //{ // t.testTag01 = new testTag01(); // t.testTag01.NV = "123123";//Not Recorded //}
This code generates <testTag.01>SomeValue</testTag.01>
or <testTag.01 xsi:nil="true"/>
.
If I uncomment the ELSE, the result would be: <testTag.01>SomeValue</testTag.01>
or <testTag.01 NV="123123" />
.
So I have no idea how to get to the format, which is required by the validation tool. Any ideas ?
P.S.
Here is the auto-generated C# class:
/// [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.blabla.org")]
public partial class testTag01 {
private string nvField; private SomeEnum valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string NV { get { return this.nvField; } set { this.nvField = value; } } /// <remarks/> [System.Xml.Serialization.XmlTextAttribute()] public SomeEnum Value { get { return this.valueField; } set { this.valueField = value; } } }
I wouldn't like to alter that part, but I understand it is impossible without doing it. Also I have tried to set SomeEnum to be Nullable. public SomeEnum? Value
, but is throwing an exception:
Cannot serialize member 'Value' of type System.Nullable`1[]. XmlAttribute/XmlText cannot be used to encode complex types.