mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-26 12:08:37 +08:00
added classes to handle local connection properties when dealing with the DB
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace mRemoteNG.Config.Serializers.MsSql
|
||||
{
|
||||
public class LocalConnectionPropertiesModel
|
||||
{
|
||||
public string ConnectionId { get; set; }
|
||||
public bool Connected { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace mRemoteNG.Config.Serializers.MsSql
|
||||
{
|
||||
public class LocalConnectionPropertiesXmlSerializer :
|
||||
ISerializer<IEnumerable<LocalConnectionPropertiesModel>, string>,
|
||||
IDeserializer<string, IEnumerable<LocalConnectionPropertiesModel>>
|
||||
{
|
||||
public string Serialize(IEnumerable<LocalConnectionPropertiesModel> models)
|
||||
{
|
||||
var localConnections = models
|
||||
.Select(m => new XElement("Node",
|
||||
new XAttribute("ConnectionId", m.ConnectionId),
|
||||
new XAttribute("Connected", m.Connected)));
|
||||
|
||||
var root = new XElement("LocalConnections", localConnections);
|
||||
var xdoc = new XDocument(new XDeclaration("1.0", "utf-8", null), root);
|
||||
return WriteXmlToString(xdoc);
|
||||
}
|
||||
|
||||
public IEnumerable<LocalConnectionPropertiesModel> Deserialize(string serializedData)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(serializedData))
|
||||
return Enumerable.Empty<LocalConnectionPropertiesModel>();
|
||||
|
||||
var xdoc = XDocument.Parse(serializedData);
|
||||
return xdoc
|
||||
.Descendants("Node")
|
||||
.Where(e => e.Attribute("ConnectionId") != null)
|
||||
.Select(e => new LocalConnectionPropertiesModel
|
||||
{
|
||||
ConnectionId = e.Attribute("ConnectionId")?.Value,
|
||||
Connected = bool.Parse(e.Attribute("Connected")?.Value ?? "False")
|
||||
});
|
||||
}
|
||||
|
||||
private static string WriteXmlToString(XNode xmlDocument)
|
||||
{
|
||||
string xmlString;
|
||||
var xmlWriterSettings = new XmlWriterSettings { Indent = true, IndentChars = " ", Encoding = Encoding.UTF8 };
|
||||
var memoryStream = new MemoryStream();
|
||||
using (var xmlTextWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
|
||||
{
|
||||
xmlDocument.WriteTo(xmlTextWriter);
|
||||
xmlTextWriter.Flush();
|
||||
var streamReader = new StreamReader(memoryStream, Encoding.UTF8, true);
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
xmlString = streamReader.ReadToEnd();
|
||||
}
|
||||
return xmlString;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,6 +159,8 @@
|
||||
<Compile Include="Config\ILoader.cs" />
|
||||
<Compile Include="Config\Import\MRemoteNGCsvImporter.cs" />
|
||||
<Compile Include="Config\ISaver.cs" />
|
||||
<Compile Include="Config\Serializers\ConnectionSerializers\MsSql\LocalConnectionPropertiesModel.cs" />
|
||||
<Compile Include="Config\Serializers\ConnectionSerializers\MsSql\LocalConnectionPropertiesXmlSerializer.cs" />
|
||||
<Compile Include="Config\Serializers\CredentialProviderSerializer\CredentialRepositoryListDeserializer.cs" />
|
||||
<Compile Include="Config\CredentialRepositoryListLoader.cs" />
|
||||
<Compile Include="Config\Serializers\CredentialSerializer\XmlCredentialPasswordDecryptorDecorator.cs" />
|
||||
|
||||
Reference in New Issue
Block a user