tags:

views:

9

answers:

1

I need to create an xml file like this:

<AGENDA>
 <COVER COLOR>BLACK</COVER COLOR>
 <PRICE>24.99</PRICE>
 <ENTRIES>
  <NAMESURNAME>AAABBB</NAMESURNAME>
  <PHONENUMBER>3434534543</PHONENUMBER>
  <NAMESURNAME>EEEAAA</NAMESURNAME>
  <PHONENUMBER>2342342</PHONENUMBER>
 </ENTRIES>
</AGENDA>

That is, one or more options and a complex type in sequence. I need only one set of options per xml file while one or more entries. I use the xsd text editor embedded in VS2010 but I don't know how to properly place the options elements. If I type (after the <xsd:schema....> tag):

 <xsd:element name="OPTION1" type="xsd:string"/>
  <xsd:element name="RawPolling">
    <xsd:complexType>
      <xsd:sequence maxOccurs="unbounded" minOccurs="0">
        <xsd:element name="RawPollingEntry">
          <xsd:complexType>
            <xsd:sequence maxOccurs="1" minOccurs="0">
              <xsd:element name="Nome" type="xsd:string" />
.....
....

I do not get any error on xsd but when I write the xml I get "XML document cannot contain multiple root level elements" I tried to move the OPTION1 inside the RawPolling (but not inside the RawPollingEntry, OPTION1 is not part of a complex/sequence type, I need only one OPTION1 per xml file) and get other errors ("the element ... has invalid child element...)

How can I accomplish this?

A: 

First of all, your XML in not valid - you cannot have tags with spaces in their names, so this:

<COVER COLOR> ..... </COVER COLOR>

is invalid to begin with.

Next - .NET comes with a great tool called xsd.exe. In this case, I made your XML valid (changed the tag to <COVERCOLOR>) and then ran xsd.exe on it - this produces an XML schema file based on the contents of your XML file.

The result here is this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="newschema" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="AGENDA">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="COVERCOLOR" type="xs:string" minOccurs="0" />
        <xs:element name="PRICE" type="xs:string" minOccurs="0" />
        <xs:element name="ENTRIES" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="NAMESURNAME" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="NAMESURNAME_Text" msdata:Ordinal="0">
                    <xs:extension base="xs:string">
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="PHONENUMBER" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="PHONENUMBER_Text" msdata:Ordinal="0">
                    <xs:extension base="xs:string">
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

You should find your xsd.exe in a path something similar to this:

C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Bin\

This is on a 64-bit Windows 7 machine - on 32-bit, it would be c:\program files\... instead.

marc_s