Skip to content

Commit c4caa22

Browse files
authored
Merge pull request #7 from Broda/master
Null warning cleanup and String/Xml Extension creation
2 parents 7106528 + 21eb885 commit c4caa22

18 files changed

+436
-694
lines changed

CnE2PLC.Helpers/StringHelper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CnE2PLC.Helpers
6+
{
7+
public static class StringHelper
8+
{
9+
public static int ContainsAsBit(this string? str, string searchStr)
10+
{
11+
return (str ?? String.Empty).Contains(searchStr) ? 1 : 0;
12+
}
13+
14+
public static T? ParseEnum<T>(this string str) where T : struct, Enum
15+
{
16+
return Enum.TryParse<T>(str, out var t) ? t : (T?)null;
17+
}
18+
}
19+
}

CnE2PLC.Helpers/XMLHelper.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Xml;
5+
using System.Xml.Linq;
6+
7+
namespace CnE2PLC.Helpers;
8+
9+
public static class XMLHelper
10+
{
11+
public static XmlNode CreateGenericXmlNode()
12+
{
13+
XmlDocument document = new XmlDocument();
14+
return document.CreateNode(XmlNodeType.Element, "Generic", "");
15+
}
16+
17+
public static string GetNamedAttributeItemValue(this XmlNode node, string name)
18+
{
19+
return node.Attributes?.GetNamedItem(name)?.Value ?? String.Empty;
20+
}
21+
22+
public static string GetNamedAttributeItemInnerText(this XmlNode node, string name)
23+
{
24+
return node.Attributes?.GetNamedItem(name)?.InnerText ?? String.Empty;
25+
}
26+
27+
public static int? GetNamedAttributeItemInnerTextAsInt(this XmlNode node, string name)
28+
{
29+
return int.TryParse(node.GetNamedAttributeItemInnerText(name), out var value) ? value : null;
30+
}
31+
32+
public static DateTime? GetNameAttributeItemInnerTextAsDateTime(this XmlNode node, string name)
33+
{
34+
string input = node.GetNamedAttributeItemInnerText(name);
35+
var sp = input.Split(' ');
36+
input = sp[1] + " " + sp[2] + ", " + sp[4] + " " + sp[3];
37+
return DateTime.TryParse(input, out DateTime dt) ? dt : null;
38+
// Sat May 11 10:43:06 2024
39+
40+
}
41+
42+
public static XmlNode SelectSingleNode(this XmlNode node, string name, XmlNode defaultIfNull)
43+
{
44+
return node.SelectSingleNode(name) ?? defaultIfNull;
45+
}
46+
47+
public static string GetChildNodeInnerText(this XmlNode node, string name, bool useLast)
48+
{
49+
XmlNode? child = useLast ? node.ChildNodes.Cast<XmlNode>().LastOrDefault(n => n?.Name == name, null) : node.ChildNodes.Cast<XmlNode>().FirstOrDefault(n => n?.Name == name, null);
50+
return child == null ? String.Empty : child.InnerText;
51+
}
52+
53+
public static bool AttributeExists(this XmlNode node, string name)
54+
{
55+
return node.Attributes?.GetNamedItem(name) != null;
56+
}
57+
58+
public static bool AttributeExists(this XmlNode node, string name, out XmlNode attribute)
59+
{
60+
XmlNode? temp = node.Attributes?.GetNamedItem(name);
61+
if (temp != null)
62+
{
63+
attribute = temp;
64+
return true;
65+
}
66+
attribute = CreateGenericXmlNode();
67+
return false;
68+
69+
}
70+
}

CnE2PLC.PLC/CnE2PLC.PLC.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
<ItemGroup>
1010
<PackageReference Include="libplctag" Version="1.5.2" />
11-
<PackageReference Include="System.Xml.XDocument" Version="4.3.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\CnE2PLC.Helpers\CnE2PLC.Helpers.csproj" />
1215
</ItemGroup>
1316

1417
</Project>

0 commit comments

Comments
 (0)