How to make XSD attributes "required"

Last post 07-06-2009 11:37 AM by johnwsaunders3. 7 replies.

Sort Posts:

  • How to make XSD attributes "required"

    07-01-2009, 4:30 PM
    • Member
      point Member
    • hshah79
    • Member since 07-01-2009, 8:25 PM
    • Posts 3

    I am generating XSD from the below class. How do I make the FirstName and LastName attributes "Required" when it generates the xsd.

    <Serializable()> _
    <XmlRoot("Persons")> _
    Public Class Persons

        Public Sub New()
            Items = New List(Of PersonsDetails)()
        End Sub

        Private _Items As List(Of PersonsDetails)
        <XmlElement("Person")> _
        Public Property Items() As List(Of PersonsDetails)
            Get
                Return _Items
            End Get
            Set(ByVal value As List(Of PersonsDetails))
                _Items = value
            End Set
        End Property
    End Class


    Public Class PersonsDetails

        Private _fName As String
        Private _lName As String
        Private _city As String
        Private _state As String
        Private _id As Integer


        <XmlAttribute("ID")> _
        Public Property ID() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property


        <XmlAttribute("FirstName")> _
       Public Property FirstName() As String
            Get
                Return _fName
            End Get
            Set(ByVal value As String)
                _fName = value
            End Set
        End Property


        <XmlAttribute("LastName")> _
        Public Property LastName() As String
            Get
                Return _lName
            End Get
            Set(ByVal value As String)
                _lName = value
            End Set
        End Property


        <XmlAttribute("City")> _
        Public Property City() As String
            Get
                Return _city
            End Get
            Set(ByVal value As String)
                _city = value
            End Set
        End Property


        <XmlAttribute("State")> _
        Public Property State() As String
            Get
                Return _state
            End Get
            Set(ByVal value As String)
                _state = value
            End Set
        End Property
    End Class

  • Re: How to make XSD attributes "required"

    07-02-2009, 12:38 AM
    • All-Star
      26,951 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 9:49 AM
    • Mumbai
    • Posts 4,561

    Hi,
    //u may use ISnullable=True
    http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute.aspx

    http://msdn.microsoft.com/en-us/library/bfdchewb(VS.71).aspx

    Check teh above link...

    Or

    u may also Use Xsd.exe..

    http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: How to make XSD attributes "required"

    07-02-2009, 8:06 AM
    • Member
      point Member
    • hshah79
    • Member since 07-01-2009, 8:25 PM
    • Posts 3

    That did not work. Below is the XSD that gets generated programmatically (I am not using xsd.exe) using the above class. I want the FirstName and LastName attributes to be "required" like the ID attribute?

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Persons" nillable="true" type="Persons" />
      <xs:complexType name="Persons">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="unbounded" name="Person" type="PersonsDetails" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="PersonsDetails">
        <xs:attribute name="ID" type="xs:int" use="required" />
        <xs:attribute name="FirstName" type="xs:string" />
        <xs:attribute name="LastName" type="xs:string" />
        <xs:attribute name="City" type="xs:string" />
        <xs:attribute name="State" type="xs:string" />
      </xs:complexType>
    </xs:schema>

  • Re: How to make XSD attributes "required"

    07-04-2009, 9:26 AM

    There is nothing you can do with ASMX technology to make a reference type emit "required" for an attribute, or minOccurs="1" for an element.

    You can do this using the Data Contract Serializer with WCF. You may want to plan on moving to WCF: see ASMX Web Services are a “Legacy Technology”.

    John Saunders
  • Re: How to make XSD attributes "required"

    07-04-2009, 2:31 PM
    • All-Star
      26,951 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 9:49 AM
    • Mumbai
    • Posts 4,561

    hshah79:

    That did not work. Below is the XSD that gets generated programmatically (I am not using xsd.exe) using the above class. I want the FirstName and LastName attributes to be "required" like the ID attribute?

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Persons" nillable="true" type="Persons" />
      <xs:complexType name="Persons">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="unbounded" name="Person" type="PersonsDetails" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="PersonsDetails">
        <xs:attribute name="ID" type="xs:int" use="required" />
        <xs:attribute name="FirstName" type="xs:string" />
        <xs:attribute name="LastName" type="xs:string" />
        <xs:attribute name="City" type="xs:string" />
        <xs:attribute name="State" type="xs:string" />
      </xs:complexType>
    </xs:schema>


    What R U trying May not Posssible.

    http://stackoverflow.com/questions/646216/in-xsd-how-can-i-define-two-different-elements-with-the-same-name

    Check The above Link for Ur Solution.

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: How to make XSD attributes "required"

    07-04-2009, 10:23 PM

    I don't see how that addresses his issue at all. He wants to make an attribute be required.

    John Saunders
  • Re: How to make XSD attributes "required"

    07-06-2009, 9:44 AM
    • Member
      point Member
    • hshah79
    • Member since 07-01-2009, 8:25 PM
    • Posts 3

    I am still waiting for a satisfactory solution on how to make an attribute to be "required"?

  • Re: How to make XSD attributes "required"

    07-06-2009, 11:37 AM
    Answer

    As I said, it is not possible.

    John Saunders
Page 1 of 1 (8 items)