views:

75

answers:

2

I used the xsd utility to generate a *.cs file from an *.xsd file. I'd like to generate xml from this generated class by serializing an instance of the class. Is there any way to get 'clean' output like this:

<header>
  <br/>
  <br/>
  <br/>
  <br/>
</header>

Here are two examples of the not clean output I am getting:

<header>
  <br xsi:type="xsd:string" />
  <br xsi:type="xsd:string" />
  <br xsi:type="xsd:string" />
  <br xsi:type="xsd:string" />
</header>

<header>
  <br xsi:nil="true" />
  <br xsi:nil="true" />
  <br xsi:nil="true" />
  <br xsi:nil="true" />
</header>

Running this code to create the object being serialized:

KioskSchema.applicationScreens screenContainer = new KioskSchema.applicationScreens();
//screenContainer.header = new object[] { null, null, null, null };                                     //didn’t work
//screenContainer.header = new string[] { "<br/>", "<br/>", "<br/>", "<br/>"};               //didn’t work
screenContainer.header = new string[] { string.Empty, string.Empty, string.Empty, string.Empty };       //didn’t work

Here is the class generated from the xsd utility

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class applicationScreens
{

       private object[] headerField;

       private applicationScreensScreen[] screenField;

       /// <remarks/>
       [System.Xml.Serialization.XmlArrayItemAttribute("br", IsNullable = false)]
       public object[] header
       {
              get
              {
                     return this.headerField;
              }
              set
              {
                     this.headerField = value;
              }
       }
}
A: 

Looks like a namespace is being added when you don't want it to.

Change the XmlArrayItemAttribute in the generated .cs file - add Namespace = null or Namespace = string.Empty:

[System.Xml.Serialization.XmlArrayItemAttribute("br", IsNullable = false, Namespace = string.Empty)]

That should override the default namespace that is being added.

Read more about XmlArrayItemAttribute on MSDN.

Oded
A: 

The xsd utility set the type of the header propery and it's private member varible to object[]. Manually changing them to string[] fixed the problem!

public partial class applicationScreens
{

 private string[] headerField;

 private applicationScreensScreen[] screenField;

 /// <remarks/>
 [System.Xml.Serialization.XmlArrayItemAttribute("br", Namespace="",  IsNullable = false)]
 public string[] header
 {
  get
  {
   return this.headerField;
  }
  set
  {
   this.headerField = value;
  }
 }

Actual output:

<header>
  <br />
  <br />
  <br />
  <br />
</header>
Joe T