2

I have a C# class member like so:

class Case
{
  string ID;
  string JurisdictionID;
}

the corresponding XSD looks like:

<xs:element name="CaseTrackingID" type="IDType"/>

<xs:complexType name="IDType">
  <xs:sequence>
    <xs:element name="ID" type="xs:string"/>
    <xs:element name="IDJurisdictionText" type="xs:string" minOccurs="0"/>
    <xs:element name="IDType" type="xs:string" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

How would I annotate my class so that CaseTrackingID\ID maps to Case.ID and CaseTrackingID\IDJurisdictionText maps to Case.JurisdictionID?

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Jeff Swensen
  • 3,513
  • 28
  • 52

2 Answers2

2

Try this:

[XmlRoot("CaseTrackingID")]
public class Case
{
    [XmlElement("ID")]
    public string ID;

    [XmlElement("IDJurisdictionText")]
    public string JurisdictionID;
}
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

When the schema and class structures don't quite line up you have a couple of options, none of them that pretty..

public class Case
{
    [XmlIgnore] public string ID;
    [XmlIgnore] public string JuristictionID;

    [XmlElement("CaseTrackingID")]
    public CaseTrackingID SerializedCaseTrackingID
    {
      get 
      { 
          return new CaseTrackingID 
          { 
              ID = this.ID, 
              JuristictionID = this.JuristictionID,
          }; 
      }
      set 
      { 
          this.ID = value.ID; 
          this.JuristictionID = value.JuristictionID;
      }
    }
}
public class CaseTrackingID 
{
    [XmlElement("ID")]
    public string ID;

    [XmlElement("IDJurisdictionText")]
    public string JurisdictionID;
}

Or, if you're using DCSerializer, you can use the surrogate feature (http://msdn.microsoft.com/en-us/library/ms733064.aspx) to substitute a different class structure at serialization time.

alexdej
  • 3,751
  • 23
  • 21