Creating problems for users. One of them is the XSD extension. It is quite rare in practice, so not all applications are able to open it. Let's talk about what this “monster” is and what utility can be used to unpack it.

Selecting an application to work with a file with the XSD extension.

The XSD file format is used to define the type of data that appears in XML file. It also establishes the relationship between various parameters. It can also be created by the Pattern Maker for Cross stitch program, which serves as an assistant for beginners in cross stitch. This software is the most popular among such people.

XSD: how to open

Let's take a closer look at what programs can open XSD. There are quite a large number of them, but we will look at the most basic ones.


Of course, to carry out an operation to open of this format possible with the help of others professional programs. However, downloading them solely for this purpose is stupid and pointless.


As with XML editors, you can open XSD using other programs. If you decide to use Word, then this will not be a particularly correct decision, since the data will be displayed in it exactly the same as in Notepad.

As mentioned at the beginning of the article, XSD is widely used by people involved in embroidery. In this case, the file is presented as a picture. To open and edit it, only one program has been created - Pattern Maker for Cross-stitch. In it you can see a palette of colors and descriptions for embroidery. This software is very multifunctional, absolutely free and can be Russified without any problems.

Conversion to other formats

Due to the structure of this type files, it is impossible to convert them to other formats. However, if you are working with an embroidery pattern, then it can be converted to JPG without any problems using Pattern Maker for Cross-stitch. To do this, select “Export to” from the application menu. graphic file", then activate "Export diagram" and "Export information".

Thus, you have become acquainted with the XSD format, which can represent not only text information, but also an image. Of course, it is not widespread among ordinary users, but for people who are passionate about embroidery, this extension is necessary. We hope you don't have any questions. If you have them, then ask them in the comments.

Annotation: XML Data Schemas (XSD). Creating an XSD Schema in the Environment Visual Studio.NET. Creating a typed DataSet object. Obtaining information about the structure of a DataSet object. Loading XML documents and XSD schemas into typed and regular DataSet objects. ReadXml and ReadXmlSchema methods. Recording the content and structure of a typed and regular DataSet object. WriteXml and WriteXmlSchema methods

Typed DataSet object. Reading and writing XML documents

XML Data Schemas (XSD)

Data schemas 1 Exact translation of XML Schema Document -< XML document schema>, however we will continue to use the term "XML data schema". XSD (XML Schema Document, XSD) are alternative way rules for constructing XML documents. Compared to DTDs, schemas are more powerful for defining complex data structures, provide a clearer way to describe the grammar of a language, and can be easily modernized and extended. An XSD schema can contain the following information:

  • representation of relationships between data elements, similar to foreign key relationships between tables in a relational database;
  • representation of unique identifiers similar to a primary key;
  • type specification data for each individual element and attribute in an XML document.

To create a data schema in Visual Studio .NET, go to File\New\File in the main menu and select XML Schema from the list of templates (see Fig. 11.3). A window appears that says "To begin, drag objects from the Server Explorer or Toolbox window onto the workspace (design area) or click right click" (Fig. 11.1):


Rice. 11.1.

The point is that we are in design mode. Switch to code mode by clicking on the button (XML):

The first line is the familiar indication that the schema is an XML document with a root element xs:schema . The xs prefix: prefixes all schema elements to indicate its namespace. The second line contains a long, detailed and often completely unnecessary description of the schema (compare with similar code for HTML pages). For correct operation, it is enough to limit yourself to the following representation:

However, the studio's built-in visualization tools assume the presence of this “header”, so it should not be removed. Creating a schema that describes a given XML document in Visual Studio .NET is a fairly simple task. Let's create next document XMLEasy.xml:

1

We switch to the (Data) tab and see only one entry (Fig. 11.2).


Rice. 11.2.

You can create a schema that describes this document in several ways: in the main menu, select “XML\Create Schema” (Fig. 11.3, A), in XML mode in context menu select the same item (Fig. 11.3, B), in Data mode select this item in the context menu (Fig. 11.3, C), and finally, in Data mode, click on the XML toolbar button (Fig. 11.3, D).


Rice. 11.3.

In any case, the document outline appears in the form of a table (Fig. 11.4). Let's leave Schema mode for now and switch to (XML) mode.


Rice. 11.4.

The environment generated XML code describing the structure of the document:

This also includes the description needed to further manipulate the schema using ADO .NET objects. In the source XMLEasy.xml document, a link to the data schema appeared:

The XMLEasy.xsd document was automatically created in the same directory where XMLEasy.xml is located.

In order to learn to understand XSD schemas, you should first work with the description of the data in its pure form, without additional elements. Table 11.1 shows several simple XML documents and their schemas, generated without binding to ADO .NET objects.

Table 11.1. Examples of creating XSD schemas
Contents of the XML document XSD Schema Contents

1

Description
In an XMLEasy.xml document, the TOUR element is the root element containing the IDTOUR child element. The general scheme for the root element is as follows:

: Element content...

Child elements are described like this:

Contents of the XML document XSD Schema Contents

1 Cyprus RUB 25,000.00 The price of two adult vouchers includes the price of one child (up to 7 years old) 2 Greece RUB 32,000.00 Special discounts apply in August and September 3 Thailand RUB 30,000.00 Not including airfare 4 Italy RUB 26,000.00 Breakfast at the hotel is included in the price of the tour 5 France RUB 27,000.00 Additional excursions are not included in the price of the tour

Description
The root TABLE element contains a TOUR element, which in turn consists of a group of child elements. The choice element controls the selection of other elements, with the "unbounded" value of the maxOccurs attribute indicating that TOUR groups can be unlimited.

: :

Contents of the XML document XSD Schema Contents

1 Cyprus RUB 25,000.00 1 The price of two adult vouchers includes the price of one child (up to 7 years old)


Rice. 11.5.
Description
The IDTOUR element's data type was set to int , the CLOSED element's data type was set to boolean , and the rest were set to the default string data type. You can change the data type directly in the XML data schema mode, but it is more convenient to use the Schema mode (in this case the mode will be called DataSet) select the data type from the drop-down list (Fig. 11.5):

IN software For the course you will find all the files of this table in the XSD folder (Code\Glava5\XSD).

Setting a data type in an XML document (Table 11.1 is the latest example) is one way to limit the content. Additional attributes are used to constrain the value of a given type. In the following schema fragment, the value of the PRICE element must be between 50 and 100:

To limit an XML document to some fixed values, use the following construct:

Here the NAME element can take only one fixed value out of five country names.

Developing an XSD schema is quite a lot of work. The visual tools in Visual Studio .NET make this task much easier. To master the basic concepts, it is advisable to learn several automatically generated XML document schemas. Tables 11.2-11.4 provide a description of the main elements and attributes that can be encountered.

Table 11.2. XSD Schema Elements
Element Description
all Nested elements can be defined in any order
annotation Parent element of comment elements And
any Any nested elements
anyAttribute Any attributes
appInfo Comment element. Specifies the schema title
attribute Attribute
attributeGroup Attribute group
choice Selecting other elements. Analogue of the operator "|" in DTD
complexContent Restrictions or model extensions complex content type
complexType Complex element
documentation Comment element. Provides information about the schema
element Element
extension Element extensions
field Field declaration. Applies inside an element to define fields
group Group of elements
import Importing a type declaration from another schema
include Including a different schema in an existing namespace
key Specifying an element or attribute with a key pointing to another element
keyref Specifying the element or attribute that the key points to
list An element that can contain a list of values
redefine Overriding already declared elements
restriction Element constraint
schema Schema root element
selector Selector for selecting XML elements
sequence Sequence of other elements. Analog of operator "," in DTD
simpleContent A model whose contents represent only character data
simpleType Simple element
union An element or attribute that can have multiple meanings
unique An element or attribute that must have a unique value
Table 11.3. Attributes - XSD Schema Limitations
Attribute Description
enumeration List of values
length Length
maxLength Maximum length
minLength Minimum length
maxExclusive Maximum value
maxInclusive Maximum value inclusive
minExclusive Minimum value
minInclusive Minimum value inclusive
fractionDigits Number of decimal places in fractional numbers
totalDigits Number of digits
pattern Sample (pattern) of element contents
default Default element or attribute value
elementFormDefault Setting properties of a local element as globally defined
fixed Fixed element or attribute value
form Locally declared elements are defined in specific document instances
itemType List Item Type
memberTypes Type of members used in union
maxOccurs Maximum number of occurrences of an element
minOccurs Minimum number of occurrences of an element
mixed Specifying an element that has a mixed type
name Element or attribute name
namespace Namespace
noNamespace Specifying the location of the schematic document,
SchemaLocation having no resulting namespaces
nillable Determining that an element can have an empty NULL value (nil)
ref Setting a reference to a globally defined element
schemaLocation Locating the circuit
substitutionGroup Defining the replacement of elements with other elements
targetNamespace Resulting schema namespace
type Item type
use Is the element required or not?
value Schematic element value
xsi:nil Setting the actual content of a null (NULL) element in an XML document
xsi:schemaLocation The actual location of the element in the XML document
xsi:type The actual type of the element in the XML document

In the previous article about XML, we looked at such an outdated method of validating XML documents as the XML DTD. This method is still used to validate XML data, but every day it is increasingly being replaced by a new technology called XML Schema. Many shortcomings that were in the XML DTD have been corrected in XML schemas, so at the moment all leading developers use only XML schemas for document validation.

To appreciate the advantages of XML Schemas over DTDs, let's take a closer look at the main disadvantages of DTDs that have been successfully corrected in XML Schemas. I already mentioned them in the article “”, but for a better understanding, let’s repeat.

Disadvantages of XML DTD over XML Schema

  1. Language syntax different from XML. That is, DTD is not XML. In this regard, various problems may arise with the encoding and verification of XML documents.
  2. No data type checking. There is only one data type in an XML DTD—string. In this regard, for example, if there is text in a numeric field, the document will still pass verification, since the XML DTD cannot check the data type.
  3. You cannot assign more than one DTD description to one XML document. That is, a document can be verified with only one DTD description. If there are several of them, you will have to redo the descriptions and combine everything in one file, which is very inconvenient.

These were the main shortcomings of the XML DTD, which were successfully corrected in the industry standard for describing XML documents, XML Schema.

XML Schema is an industry standard for describing XML documents

In short, XML Schema does the following:

  1. Describes the names of elements and attributes (dictionary).
  2. Describes the relationship between elements and attributes, as well as their structure (content model).
  3. Describes data types.

I also want to note that at the moment almost everything can be described using diagrams. That is, a schema is a universal way of describing the grammar of data, which can be used not only for verifying XML documents, but also for describing databases, etc. Thus, the scope of application of the schemes is currently very wide.

Example XML Schema for validating an XML document

As practice shows, the material is absorbed much better if you immediately start studying with examples. I’ll say right away that we won’t go into all the details, since the material is very complex, especially if you study it in text form.

Example of a simple XML schema

Using this schema, you can validate the following XML document.

<книга xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "book.xsd"> <название>XML Basics <цена>300

So let's go back to our XML schema. As you have already noticed, XML, already known to us, is used to create XML schemas. The only difference here is that XML Schema has elements already defined, unlike regular XML. In this regard, namespaces are used. In this case, the required namespace will be “http://www.w3.org/2001/XMLSchema”, which will be specified using the “xs” prefixes.

I would like to note right away that you can use both namespace prefixes and set a namespace for the root element. There is no difference as such. Everyone decides for themselves what to do in a given situation. Let me just say that the prefixes “xs” or “xsd” are usually used.

Well, now let's figure out how to decipher the above diagram. As mentioned above, XML Schema is a description of a vocabulary and data types. Based on this, we will decipher each element.

  • — declare the “book” element with the “Book” type.
  • — declare a complex type with the name “Book” (xs: complexType - can contain nested elements).
  • — declaration of nesting. That is, the type will contain nested elements.
  • — declare an element with the name “title” (standard type “string” - xs:string).
  • — declare an element with the name “price” (standard type “number” - xs:decimal).

As you can see, there is nothing super complicated here. If you think about it, everything is very simple.

Basic XML Schema Elements

To be brief, the XML schema can be described as follows.

Your scheme

As you can see from the example, each XML schema consists of a root element “schema” and a required namespace “http://www.w3.org/2001/XMLSchema”. Next comes a description of the circuit and the circuit itself. At the same time, very often in very high-quality schemas the description is much larger than the XML Schema itself.

Description of elements in XML Schema

At the beginning of the article, we already looked at an example of a simple XML schema. In it we separately described elements and types. At the same time, I would like to immediately note that the sequence does not play a role here. The scheme will work in any case.

Now let's look at the second way of writing XML Schema, which is based on describing the type immediately inside the element. This method is suitable if you do not plan to use the same description for different elements. For clarity, let's look at an example.

As can be seen from the example, an element is declared using a special construction “element” using the appropriate prefix. In this case, we define an element called “root” without specifying a type, since it will be described inside the element. That is, there are two ways to describe elements.

1 way

Type description

Method 2

Description of element type

You can use both the first method and the second. They all work the same way. The only question is convenience in this particular case.

Further, after declaring the element, we indicate that it is of complex type ( ) and list ( ) nested elements. In this case, these are the name and age elements with types “xs:string” and “xs:integer”. The xs prefix means that it is a base type that is already defined in the XML Schema standard.

As you can see, everything is quite simple so far. Again, we will not go into all the details, since this article is intended to familiarize you with XML Schema, and not to study it in detail.

How XML Schema and document are matched

The peculiarity of XML Schema is that it describes not the document itself, but the namespace. In this regard, most often there is no mention of it in the document. The handler itself maps the schema you need without using any instructions in the XML document.

In case the handler does not know where the schema is, we can indicate where to look for it. This is done using the special attribute “schemaLocation”. Since this attribute belongs to a different namespace, the namespace must also be specified before using the attribute. For clarity, let's look at an example.

XML Schema

XML document

Now let's look at each line in detail.

  • targetNamespace=”http://www.site.com” – indicate which namespace this XML Schema is for.
  • xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” – we connect the namespace in which the “schemaLocation” attribute is described.
  • xsi:schemaLocation=”http://www.site.com/product.xsd” – we indicate where the schema can be found in case the parser does not know where it is. If the XML document does not belong to any namespace, and therefore there is no reference to it in the schema, then the “schemaLocation” attribute is replaced with “noNamespaceSchemaLocation” (a reference to the schema without defining namespaces).

That's all. Good luck and success in learning XML!

A file with an XSD extension is most likely an XML schema file; a text file format that defines the validation rules for an XML file and explains the XML form.

Because XSD files are schema files, they provide a model for something else, in this case XML files. For example, an XSD file may require the XML file to have certain boundaries, relationships, ordering, attributes, nested characteristics, and other elements, and may set any restrictions.

XML files can reference an XSD file with a schemaLocation attribute.

HobbyWare's Pattern Maker cross stitch program also uses the XSD extension for its format.

How to open an XSD file

XSD files can also be opened using Microsoft Visual Studio, XML Notepad, EditiX XML and XMLSpy. Oxygen XML Editor is one of the few XSD tools that works on Linux, Mac and Windows.

You can also use a text editor as a means to view and edit the XSD, given that the file is just a text file.

Here's an example of what the XSD file looks like when opened in a text editor:








If you are dealing with an XSD file used with Pattern Maker, you can of course open it with that software. However, HobbyWare offers the Pattern Maker Viewer to open and print a pattern file for free. Simply drag and drop the XSD file into the program or use the menu "File""Open...". This viewer also supports the similar PAT format.

The Crossty app for iOS and Android can also open XSD files for cross stitch, but it's not free.

How to convert an XSD file

The easiest way to convert an XSD file to another format– use one of the XSD editors described above.

Another conversion you may need is XSD to PDF so that you can open the XSD file in Adobe Acrobat Reader or some other tool. There's probably no particular reason to convert an XSD file to PDF other than to make sure the code is visible on any computer that opens it.

XML Schema Definition Tool can convert XDR, XML and XSD files to a serializable class or data set as # a C class.

You can use Microsoft Excel if you need to import data from an XSD file and put it into an Excel spreadsheet.

It is likely that the Pattern Maker program we mentioned above (not the free viewer) can be used to convert the cross stitch XSD file to the new file format.

Still can't open the file?

If your XSD file does not open with the programs and tools listed above, there is a good chance that you are not dealing with an XSD file, but with a file with a similar extension.

For example, the XDS extension looks very similar to XSD, but is instead used for DS Game Maker project files and LcdStudio Design files. None of these file formats are associated with XML files or templates.

The same concept applies to many other file formats, such as XACT Sound Bank files, which use the .XSB file extension. These are audio files that cannot be opened using any XSD opener or file converter.

If your file doesn't end with .XSD, look into the extension to find which programs can open or convert that particular file type.

I would like to talk in one place about a very typical task when working with XML, namely, about the creation XML based on the existing one. This operation will consist of several stages, during which additional tasks will be solved, such as:

  • Creation XML based XSD, and vice versa.
  • Serialization and deserialization of objects.
  • Creating a class for future serialization from XML or XSD.
Before starting, I plan that the reader is familiar with the words XML And XSD. Concerning XML, I think there are no problems with this, but as for XSD I may have questions about what it is. In short:
XSD is a language for describing XML. And if you look at the contents of the file containing this description, the question will arise, why describe the seemingly obvious tags of your XML? But imagine that you are integrating with some external system, and you want to use the format for data exchange XML. So, XSD allows you to describe for each element its name, possible attributes, mandatory fields or attributes, and make it clear that the contents of a tag can contain only one internal element ( ) and no more, or a node may have a sequence of elements (< a> ... ). When the external system is yours, then there may be no problem, but when it is a third party, then this description through XSD, will be a tool for resolving disputes with the format, and also, using utilities, you can check the compliance of any XML original format.
So, what are we going to do to learn how to stamp XML based on your standard? I will describe everything in order.

Available:
Some kind of XML (xmlfile.xml), saved in text format to a file. Below is the text:

< catalog > < book id = " bk101 " > < author >Gambardella, Matthew < title >XML Developer's Guide < genre >Computer < price > 44.95 < publish_date > 2000-10-01 < description >An in-depth look at creating applications with XML. < book id = " bk102 " > < author >Ralls, Kim < title >Midnight Rain < genre >Fantasy < price > 5.95 < publish_date > 2000-12-16 < description >A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.

Steps to create a new one:

  1. First using the utility XSD.exe, or using online services(search for "xml to xsd" one of them www.freeformatter.com/xsd-generator.html), we will create from the existing XML its primitive XSD description. At the output we will have a completely, automatically created output.xsd a file that is already usable but may not be complete.
  2. Next, based on output.xsd , classes will be created in C# xmlclass.cs. After which, we add it to our project. You can create a class using XSD.exe or xsd2code utility, or any online service.
  3. Now we are ready to use the file classes xmlclass.cs. Therefore, let’s create and fill instances with data xmlclass, and then, using the serialization process, we will convert objects into XML line, and then save it in new file. Thus we get XML based on what is available.
Let's get started!

Step 1: Generate XSD from XML.

So, here are the methods that I use myself:
  1. Utility xsd.exe- creator Microsoft.
  2. Utility xsd2code.exe- a third-party application that can do everything the same as xsd.exe, but is available for download directly (at this moment became paid from Trial period).
  3. Use of any online services.
In order to receive XSD.exe, you will have to put any Microsoft SDKs, which comes with this application. Unfortunately, at the moment, none of the studios include this utility in their kit.

Link to download and install one of SDK:
Windows SDK for Windows 7 and .NET Framework 4.

Let's use the search and find the file xsd.exe, after which (to make our work easier), copy it to the folder created in the root of the disk C:\xmltoxsd. Next, we copy our xmlfile.xml and everything is ready to start.


We tear off " Command line"(Start -> "Run" -> "cmd"), go to our directory and call with default parameters xsd.exe, passing our xmlfile.xml as a parameter:


And we see that a file has appeared next to it xmlfile.xsd with content.


That's all! These actions are enough to create a primitive XSD file to work with. But it is worth understanding that xsd.exe, like any other utility, knows nothing about the types of your fields (so the field type will almost always be string), as well as about all variations of attributes and parameters. That is, if some tag does not have an attribute in your XML, then it won’t be in the description. That's why XML should be the most complete and if the element may contain several child fields, it is better to add at least two, so that xsd.exe I realized that this is a collection.

Syntax XSD not very complicated, so if you are writing a fairly serious description, you may have to work with a file.

Step 2: Create a Class from XSD.

To create a class in C#, we will need to use again xsd.exe but with a different parameter /classes, passing the path to our xmlfile.xsd.


After execution, the xmlfile.cs file will appear, the contents of which are shown below in compressed form:


As you can see, for the inner element catalog a class was created catalogBook, whose fields correspond to the fields in XML, and the field ID marked as an attribute. Xsd.exe I correctly understood what was inside catalog a collection of books is stored, so the property appeared Items, to populate the child collection.
Comment: xsd.exe cannot add comments to the resulting classes and fields. In description XSD there is an element called< xs:annotation > , which most often contains< xs:documentation >, inside which is a description of the field. In theory, it should be placed in the /// description, but at the moment xsd.exe refuses to add text to the description, so you have to resort to alternatives such as xsd2code or online services.

Step 3: Serialize objects to XML.

The resulting classes are in the file xmlfile.cs. Next, add its contents or the file itself to the project Visual Studio, after which you can work with it. I will create demo book objects and then save them to a file in the format XML.

Private void Example() ( // Create the first book var book1 = new catalogBook () ( author = "King" , description = "Very interesting book" , genre = "Fantasy" , price = 22.ToString(), id = "42011" , title = "It" }; !} // Create a second book var book2 = new catalogBook () ( author = "O"Brien, Tim" , description = "Microsoft's .NET initiative is explored in detail in this deep programmer"s reference.", genre = "Computer" , price = 36.ToString(), id = "30012" , title = "Microsoft .NET: The Programming Bible" }; // Create a directory root element containing the two books above var catalog = new catalog() (Items = new(book1, book2)); // Contains the XML of the catalog object var xmlCatalog = Serialize(catalog); // Write a string to a file // TODO Made for demonstration purposes. It is advisable to call the Serialize method to transfer the Stream to the file File .WriteAllText("Output.xml" , xmlCatalog); ) private string Serialize (TType sourceObject) ( if (sourceObject == null ) ( return string .Empty; ) // Use XmlSerializer to convert it to an XML string var xmlserializer = new XmlSerializer(typeof(TType)); var stringWriter = new StringWriter(); using (var writer = XmlWriter .Create(stringWriter, new XmlWriterSettings () ( Indent = true ))) ( xmlserializer.Serialize(writer, sourceObject); return stringWriter.ToString(); ) ) After running and opening Output.xml you can see that we saved it the way we wanted (Bottom file - " Created by code").