Some time ago, in one project, I had the task of importing data that was uploaded by xml files. I didn’t know where the upload came from, and it’s not important. The main thing is that everything went to a certain folder. Each xml file contained one type of information (information about one or more objects of the same type was downloaded). The project was written in C # and therefore parsing was carried out by its means.
Honestly, before that, I never worked with xml files, there was no such possibility, and here such happiness ... Naturally the word serialization for me was just a bourgeois word and so far I didn’t understand how easy it would be for me. At the beginning, the data was sampled using the XmlDocument class, and the XmlTextReader class was used to quickly determine the contents of the files. Everything was good until the variety of objects increased many times and there was a reluctance to write the parsing for each one, and I also wanted something universal and less time-consuming writing. Here serialization has become not just a word, but the subject of close study. To my luck, for all xml files, one was attached describing the structure of the stored data in the xsd format. I quickly converted this business using the xsd utility in Visual Studio to a C # source, which took several hundred kilobytes. Well, then as in all the examples on many sites:
using( FileStream stream = new FileStream(NameFile) )
{
XmlSerializer xs = new XmlSerializer(typeof(ImportObject));
ImportObject obj = (ImportObject)xs.Deserialize(stream);
}
* This source code was highlighted with Source Code Highlighter.
, obj
.
xml (null) . xml ( <Value/>). null , - . xsd, , , , . , , . . . , xml … ! .
xml , . . <Value/> 0. FileStream. Deserialize(). – Read():
class MyStream : FileStream
{
public MyStream(string NameFile)
{
}
public override int Read(byte[] array, int offset, int count)
{
…
}
}
* This source code was highlighted with Source Code Highlighter.
, – . Read , : , DateTime.MinValue, array. Deserialize(). null , xml , , . , MyStream Read() , . . Deserialize(), , . , . .
, - . .
xml , ( , ). ProgressBar, , . .
StreamReader, . . Read() 1024 , , , . StreamReader , , . , , , . , Read() 0. Deserialize(), .
P.s. (poleznij sovet) , ( ) , ( ).