0

I have a enum class like,

public enum Test
{
    a = 1,
    b = 2
}

I am creating xsd for these as below

<xs:complexType name="Test">
<xs:all>
    <xs:element name="TCode" type="TestCode" minOccurs="1" maxOccurs="1" />
    <xs:element name="TValue" type="TestValue" minOccurs="1" maxOccurs="1" />
</xs:all>
</xs:complexType>    

<xs:simpleType name="TestCode">
    <xs:restriction base="xs:string">
        <xs:enumeration value="a" />
        <xs:enumeration value="b" />
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="TestValue">
    <xs:restriction base="xs:string">
        <xs:enumeration value="1" />
        <xs:enumeration value="2" />
    </xs:restriction>
</xs:simpleType>
  1. Is this correcty way of creating xsd types for Enum class
  2. How to validate XML element without using xs:assert?

    if TCode is a then TValue is 1

    if TCode is b then TValue is 2

XML will be,

<Test>
   <TCode>a</TCode>
   <TValue>1</TValue>
</Test>

Any Ideas?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Hary
  • 5,690
  • 7
  • 42
  • 79
  • C# `enum Test` in xml looks like this: `a` or `b`. It's all! Why so complicated Xml and its Schema? What version of the Xml Schema? 1.1? If so, assert helps. key/keyref will not help with different values. – Alexander Petrov Jul 01 '15 at 15:08
  • @AlexanderPetrov, I am not using 1.1 version Sorry – Hary Jul 01 '15 at 16:07

1 Answers1

-1

I do not think this is possible using standard xsd functions. But if TCode and TValue have a fixed 1 to 1 relation, should you not better ask just one of those two? This would make it impossible to create an invalid state.

Example:

<xs:complexType name="Test">
 <xs:choise>
  <xs:element name="TCode" type="TestCode" minOccurs="1" maxOccurs="1" />
  <xs:element name="TValue" type="TestValue" minOccurs="1" maxOccurs="1" />
 </xs:choise>
</xs:complexType> 
  • This is not an answer to my question, Sorry. Else please explain in detail for why it is not possible – Hary Jul 01 '15 at 16:08
  • The idea to make just one tag is just a suggestion to improve your xsd if that is what you need. I posted this as an answer instead of a comment because I do not have the 'right' yet to do so. (I'm only a member since a few days.) Reading your question I'm assuming you need a way to create a xml format that you can validate against an xsd. If this is not the case then please provide some more information in your question of your actual goal. About the part that this is not an answer to your question; I disagree. It may not be the answer you wanted, but that's something else ... – Frederick Grumieaux Jul 02 '15 at 07:03