views:

66

answers:

1

Hi, I have an XML schema in .xdr file. This is a shortened version of this file(just for example):

<?xml version = "1.0" encoding = "WINDOWS-1252"?>  
<Schema name = "ANS.xdr"
xmlns = "urn:schemas-microsoft-com:xml-data"  
xmlns:dt = "urn:schemas-microsoft-com:datatypes">  
 <ElementType name = "Shipment" content = "eltOnly" order = "seq" model = "closed">  
  <element type = "TrackingNumber" minOccurs = "0" maxOccurs = "1"/>  
 </ElementType>  
 <ElementType name = "TrackingNumber" content = "textOnly" dt:type = "string" model = "closed"/>  
</Schema>  

Using xsd.exe to translate this file into XSD format it poduces following file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ANS.xdr" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="ANS.xdr" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Shipment">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="TrackingNumber" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Finally I am able to generate .cs file for XML serialization by: xsd.exe /c ANS.xsd which results in:

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("ANS.xdr", Namespace="", IsNullable=false)]
public partial class ANSxdr {

    private ANSxdrShipment[] itemsField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute("Shipment", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ANSxdrShipment[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <uwagi/>
[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 ANSxdrShipment {

    private string trackingNumberField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string TrackingNumber {
        get {
            return this.trackingNumberField;
        }
        set {
            this.trackingNumberField = value;
        }
    }
}

The problem is that xsd.exe adds prefix from schema name to the class name: public partial class ANSxdrShipment where shoud be just Shipment. It results in wrong tag names after serialization:

<?xml version="1.0" encoding="utf-8"?>
<ANSxdrShipment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <TrackingNumber>PN-34-23414</TrackingNumber>
</ANSxdrShipment>

I could just do manual corrects but the problem seems to aggregate when there are embedded ElementTypes. It results in extra long names, especially in complexed schemas, for example:

OpenShipmentsxdrOpenShipmentsOpenShipmentShipmentReturnNotificationDetailsReturnNotificationNotificationEMailAddress

A: 

Are you serializing partial nodes. If you serialized from root node, you should get

<?xml version="1.0" encoding="utf-8"?>
<ANSxdr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
   <Shipment>
      <TrackingNumber>PN-34-23414</TrackingNumber>
   </Shipment>
</ANSxdr>

It appears as if you serializing only Shipment object. It has not be marked with any attribute, so class name is used as element name. While ANSxdr.Items is decorated with XmlElementAttribute that will give you "Shipment" element.

EDIT: BTW, there are couple of alternatives to XSD tool:

  1. XsdCodeGen Custom Tool - this guy has also explained way to customize code generation as per your liking
  2. Sample Code Generator
VinayC