views:

9

answers:

0

I am wanting to validate a DataContract (long story short)

Here is my DataContract:

    [DataContract(Name = User.Root, Namespace = "http://mysoft.com/schemas/registeruser.xsd")]
  public class RegisterUser
  {
    [DataMember(Name = User.EmailAddress)]
    public string EmailAddress { get; set; }
    [DataMember(Name = User.UserName)]
    public string UserName { get; set; }
    [DataMember(Name = User.Password)]
    public string Password { get; set; }
    [DataMember(Name = User.FirstName)]
    public string FirstName { get; set; }
    [DataMember(Name = User.LastName)]
    public string LastName { get; set; }
    [DataMember(Name = User.PhoneNumber)]
    public string PhoneNumber { get; set; }
    [DataMember(Name = "RequestMessage")]
    public string RequestMsg { get; set; }
  }

here is my xsd:

    <xs:schema id="RegisterUser"
    elementFormDefault="qualified"
    targetNamespace="http://mysoft.com/schemas/registeruser.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:import schemaLocation=".\Types\Users.xsd" />

  <xs:element name="User">
    <xs:complexType>
      <xs:all>
        <xs:element ref="EmailAddress" />
        <xs:element ref="UserName" />
        <xs:element ref="Password" />
        <xs:element ref="FirstName" />
        <xs:element ref="LastName" />
        <xs:element ref="PhoneNumber" />
        <xs:element ref="RequestMsg" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

What seems to be happening is that the validator (according to DevStudio environment) would perfer this format:

    <User xmlns="http://mysoft.com/schemas/registeruser.xsd"&gt;
  <EmailAddress xmlns="">[email protected]</EmailAddress>
  <UserName xmlns="">MyUserName</UserName>
  <Password xmlns="">my.password</Password>
  <FirstName xmlns="">fname</FirstName>
  <LastName xmlns="">lname</LastName>
  <PhoneNumber xmlns="">555-111-2222</PhoneNumber>
  <RequestMsg xmlns="">Please help me</RequestMsg>
</User>

note the xmlns on each element. this cant be done with DataContracts (as far as I know). I have tried to remove as much namespacing as possible to work nicely with datacontracts but I still cant seem to get everything to play nicely. I would like to use the import statement and have the elements be apart of the registeruser.xsd but not need any qualifier (or namespace stuff).

This is the code I am using to test the datacontract to the xsd

public static string Serialize<T>(T obj)
{
  DataContractSerializer ser = new DataContractSerializer(obj.GetType());
  String text;
  using (MemoryStream memoryStream = new MemoryStream())
  {
    ser.WriteObject(memoryStream, obj);
    byte[] data = new byte[memoryStream.Length];
    Array.Copy(memoryStream.GetBuffer(), data, data.Length);
    text = Encoding.UTF8.GetString(data);
  }
  return text;
} 
  ...   
  string str = Serialize(reg);
  XDocument xmlData = XDocument.Parse(str);

  // Verify the xml
  XmlSchemaSet schemas = new XmlSchemaSet();
  string strXSDPath = xsdURL;
  schemas.Add(null, XmlReader.Create(strXSDPath));
  xmlData.Validate(schemas, (sender, e) =>
  {
    throw new Exception(string.Format("data failed to validate! It contained the following error:\n\n{0}", e.Message));
  }, true);