-
Notifications
You must be signed in to change notification settings - Fork 37
/
XmlFile.cs
98 lines (92 loc) · 4.48 KB
/
XmlFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//____________________________________________________________________________________________________________________________________
//
// Copyright (C) 2023, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community by pressing the `Watch` button and get started commenting using the discussion panel at
//
// https://github.com/mpostol/TP/discussions/182
//
// by introducing yourself and telling us what you do with this community.
//_____________________________________________________________________________________________________________________________________
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace TP.DataStreams.Serialization
{
/// <summary>
/// Provides static methods for serialization objects into XML documents and writing the XML document to a file.
/// </summary>
public static class XmlFile
{
#region public
/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
/// <typeparam name="type">The type of the root object to be serialized and saved in the file.</typeparam>
/// <param name="dataObject">The object containing working data to be serialized and saved in the file.</param>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <param name="mode">Specifies how the operating system should open a file <see cref="FileMode"/>.</param>
/// <param name="stylesheetName">Name of the stylesheet document.</param>
/// <exception cref="System.ArgumentNullException">
/// path
/// or
/// dataObject
/// or
/// stylesheetName
/// </exception>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
if (string.IsNullOrEmpty(stylesheetName))
throw new ArgumentNullException(nameof(stylesheetName));
if (dataObject == null)
throw new ArgumentNullException(nameof(dataObject));
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
XmlWriterSettings _setting = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n"
};
using (FileStream _docStream = new FileStream(path, mode, FileAccess.Write))
{
XmlWriter _writer = XmlWriter.Create(_docStream, _setting);
_writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" " + String.Format("href=\"{0}\"", stylesheetName));
_xmlSerializer.Serialize(_writer, dataObject);
}
}
/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
/// <typeparam name="type">The type of the object to be serialized and saved in the file.</typeparam>
/// <param name="dataObject">The object containing working data to be serialized and saved in the file.</param>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <param name="mode">Specifies how the operating system should open a file.</param>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode)
where type : IStylesheetNameProvider
{
XmlFile.WriteXmlFile<type>(dataObject, path, mode, dataObject.StylesheetName);
}
/// <summary>
/// Reads an XML document from the file <paramref name="path"/> and deserializes its content to returned object.
/// </summary>
/// <typeparam name="type">The type of the object to be deserialized and saved in the file.</typeparam>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <returns>An object containing working data retrieved from an XML file.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
public static type ReadXmlFile<type>(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
type _content = default(type);
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
FileStream _docStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using (XmlReader _writer = XmlReader.Create(_docStream))
_content = (type)_xmlSerializer.Deserialize(_writer);
return _content;
}
#endregion public
}
}