Skip to content

Commit

Permalink
Merge pull request #597 from mpostol/SemanticData630
Browse files Browse the repository at this point in the history
Semantic data630
  • Loading branch information
mpostol committed May 8, 2021
2 parents 7d63f68 + d6d7bbc commit 4aceec2
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 3,036 deletions.
1 change: 1 addition & 0 deletions Common/Infrastructure/Common.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>C:\VS.git\UAOOI\OPC-UA-OOI\Common\Infrastructure\UAOOI.Common.Infrastructure.Loc.xml</DocumentationFile>
<NoWarn>1701;1702; IDE0001</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
Expand Down
24 changes: 24 additions & 0 deletions Common/Infrastructure/Serializers/INamespaces.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//__________________________________________________________________________________________________
//
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using System.Collections.Generic;
using System.Xml;

namespace UAOOI.Common.Infrastructure.Serializers
{
/// <summary>
/// Interface INamespaces - define functionality necessary to manage namespaces for the XML serialization
/// </summary>
public interface INamespaces
{
/// <summary>
/// Gets the namespaces that is to be used to parametrize XML document.
/// </summary>
/// <returns>An instance of IEnumerable[XmlQualifiedName] containing the XML namespaces and prefixes that a serializer uses to generate qualified names in an XML-document instance.</returns>
IEnumerable<XmlQualifiedName> GetNamespaces();
}
}
28 changes: 17 additions & 11 deletions Common/Infrastructure/Serializers/XmlFile.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//____________________________________________________________________________
//__________________________________________________________________________________________________
//
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Linq;

namespace UAOOI.Common.Infrastructure.Serializers
{
Expand All @@ -17,8 +18,8 @@ namespace UAOOI.Common.Infrastructure.Serializers
/// </summary>
public static class XmlFile
{

#region public

/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
Expand All @@ -35,12 +36,14 @@ public static class XmlFile
/// stylesheetName
/// </exception>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
where type : INamespaces
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
if (dataObject == null)
throw new ArgumentNullException(nameof(dataObject));
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(dataObject.GetNamespaces().ToArray<XmlQualifiedName>());
XmlWriterSettings _setting = new XmlWriterSettings()
{
Indent = true,
Expand All @@ -52,9 +55,10 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
XmlWriter _writer = XmlWriter.Create(_docStream, _setting);
if (!string.IsNullOrEmpty(stylesheetName))
_writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" " + string.Format("href=\"{0}\"", stylesheetName));
_xmlSerializer.Serialize(_writer, dataObject);
_xmlSerializer.Serialize(_writer, dataObject, namespaces);
}
}

/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
Expand All @@ -63,10 +67,11 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
/// <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
where type : IStylesheetNameProvider, INamespaces
{
XmlFile.WriteXmlFile<type>(dataObject, path, mode, dataObject.StylesheetName);
WriteXmlFile(dataObject, path, mode, dataObject.StylesheetName);
}

/// <summary>
/// Reads an XML document from the file <paramref name="path"/> and deserializes its content to returned object.
/// </summary>
Expand All @@ -78,24 +83,25 @@ public static type ReadXmlFile<type>(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
StreamReader _docStream = new StreamReader(path);
FileStream _docStream = new FileStream(path, FileMode.Open);
return ReadXmlFile<type>(_docStream);
}

/// <summary>
/// Reads an XML document from the <paramref name="reader"/> and deserializes its content to returned object.
/// </summary>
/// <typeparam name="type">The type of the object to be deserialized.</typeparam>
/// <param name="reader">The source of the stream to be deserialized.</param>
/// <returns>An object of type <typeparamref name="type"/> containing working data retrieved from an XML stream..</returns>
public static type ReadXmlFile<type>(StreamReader reader)
public static type ReadXmlFile<type>(Stream reader)
{
type _content = default(type);
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
using (XmlReader xmlReader = XmlReader.Create(reader))
_content = (type)_xmlSerializer.Deserialize(xmlReader);
return _content;
}
#endregion

#endregion public
}
}
}
13 changes: 12 additions & 1 deletion Common/Infrastructure/UAOOI.Common.Infrastructure.Loc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4aceec2

Please sign in to comment.