<? 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 .
<? 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 .
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 .
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 .
Source: https://habr.com/ru/post/82155/
All Articles