📜 ⬆️ ⬇️

Reading XML files in .NET using XSD-generated classes

Studied at leisure code parser of mathematical expressions and saw that XML-files are read, validated and disassembled "manually" using XmlDocument.

Now, on a simple example, I will tell you how I do it.

Let's say that we should have an application that stores a list of animals. It will store it in an XML file corresponding to the following scheme:
<? xml version ="1.0" encoding ="utf-8" ? >
< xs:schema id ="XMLSchema1" targetNamespace ="http://tempuri.org/XMLSchema1.xsd" elementFormDefault ="qualified" xmlns ="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns ="http://tempuri.org/XMLSchema1.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="animals" type ="AnimalsConfiguration" />
< xs:complexType name ="AnimalsConfiguration" >
< xs:choice minOccurs ="0" maxOccurs ="unbounded" >
< xs:element name ="animal" />
< xs:element name ="anotherAnimal" />
</ xs:choice >
</ xs:complexType >
</ xs:schema >

* This source code was highlighted with Source Code Highlighter .

And, as an example, the file corresponding to this scheme:
<? xml version ="1.0" encoding ="utf-8" ? >
< animals xmlns ="http://tempuri.org/XMLSchema1.xsd" >
< animal />
< animal />
< anotherAnimal />
< animal />
</ animals >


* This source code was highlighted with Source Code Highlighter .


Required to get a list of animals contained in the file.
')
First, by the XSD scheme, you need to get an auxiliary cs class. To do this, use the utility xsd.exe.

"C: \ Program Files \ Microsoft Visual Studio 8 \ SDK \ v2.0 \ Bin \ xsd.exe" / c / l: CS / n: TryXmlRead AnimalsXMLSchema.xsd

This means creating a file in the cs format, whose classes will be located in the TryXmlRead namespace based on the AnimalsXMLSchema.xsd schema file. As a result of this utility, we get:
namespace TryXmlRead {
using System.Xml.Serialization;

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute( "xsd" , "2.0.50727.42" )]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute(Namespace= "http://tempuri.org/XMLSchema1.xsd" )]
[System.Xml.Serialization.XmlRootAttribute( "animals" , Namespace= "http://tempuri.org/XMLSchema1.xsd" , IsNullable= false )]
public partial class AnimalsConfiguration {

private object [] itemsField;

private ItemsChoiceType[] itemsElementNameField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "animal" , typeof ( object ))]
[System.Xml.Serialization.XmlElementAttribute( "anotherAnimal" , typeof ( object ))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute( "ItemsElementName" )]
public object [] Items {
get {
return this .itemsField;
}
set {
this .itemsField = value ;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "ItemsElementName" )]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName {
get {
return this .itemsElementNameField;
}
set {
this .itemsElementNameField = value ;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute( "xsd" , "2.0.50727.42" )]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace= "http://tempuri.org/XMLSchema1.xsd" , IncludeInSchema= false )]
public enum ItemsChoiceType {

/// <remarks/>
animal,

/// <remarks/>
anotherAnimal,
}
}


* This source code was highlighted with Source Code Highlighter .


Now for reading the file with the list of animals in the program it is enough to do:
using ( FileStream stream = new FileStream ( "XMLFile.xml" , FileMode .Open))
{
XmlSerializer formatter = new XmlSerializer( typeof (AnimalsConfiguration));
return (AnimalsConfiguration)formatter.Deserialize(stream);
}


* This source code was highlighted with Source Code Highlighter .


Everything, the file will be read, checked for compliance with the scheme and on its basis an object will be created with which you can later do anything.

Source: https://habr.com/ru/post/82155/


All Articles