Skip to main content

OpcUaNodeId — Strongly typed NodeId

Overview​

OPC UA node IDs are string-encoded by default in this SDK ("ns=2;s=Foo" / "i=84" / "ns=3;g=00000000-..." / "ns=4;b=AAEC"). Strings are convenient in logs and config files, but they are a trap in code:

  • Misspelled prefix (s= written as S=)
  • ns Index Drift (Server-Side Dynamic Namespace Registration)
  • GUID / Opaque Type Identification

OpcUaNodeId is readonly record struct, it explicitly includes the three components NamespaceIndex —/IdType/Identifier—and is 100% interchangeable with strings.

Corresponding section of the standard: Part 6 §5.2.2 (NodeId Coding).

API​

MembersCategoryReading and WritingNote
OpcUaNodeId.NullStaticReadEmpty Sentinel (i=0)
OpcUaNodeId.FromNumeric(ns, uint id)Factory—Constructing Numeric Types
OpcUaNodeId.FromString(ns, string id)Factory—String Constructor
OpcUaNodeId.FromGuid(ns, Guid id)Factory—Structure GUID Type
OpcUaNodeId.FromOpaque(ns, byte[] id)Factory—Constructing an Opaque Type
OpcUaNodeId.Parse(string s)Static—Parsing the string; if it fails, throw a FormatException
OpcUaNodeId.TryParse(s, out nid)Static—Security Analysis
nid.ToString()Methods—Serialize to a standard OPC UA string
nid.NamespaceIndexPropertiesReadNamespace Index (ushort)
nid.IdTypePropertiesReadNodeIdType Enumeration
nid.IdentifierPropertiesReadThe actual type of the object is determined by IdType
nid.IsNullPropertiesReadIs it empty?

Related Structures:

public enum NodeIdType
{
Numeric = 0, // i = unsigned 32-bit integer (most common)
String = 1, // s = -8 UTF string
Guid = 2, // g= GUID (D format, ToString without curly braces)
Opaque = 3 // b= base64 byte array
}

Code Example​

using System;
using System.Collections.Generic;
using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");

// 1) Factory Structure
var n1 = OpcUaNodeId.FromNumeric(0, 84); // i=84 (Root)
var n2 = OpcUaNodeId.FromString(2, "Boiler1.Temp"); // ns=2;s=Boiler1.Temp
var n3 = OpcUaNodeId.FromGuid(3, Guid.NewGuid()); // ns=3;g=xxxxxxxx-xxxx-...
var n4 = OpcUaNodeId.FromOpaque(4, new byte[] {1,2,3});

// 2) Parse the string
var n5 = OpcUaNodeId.Parse("ns=2;s=Demo.Tag");
Console.WriteLine($"ns={n5.NamespaceIndex}, type={n5.IdType}, id={n5.Identifier}");

if (OpcUaNodeId.TryParse("ns=99;i=foo", out var n6)) // false (foo is not a uint)
Console.WriteLine("ok");
else
Console.WriteLine("invalid");

// 3) Serialize back to a string
string s = n2.ToString(); // "ns=2;s=Boiler1.Temp"

// 4) Bidirectional conversion SDK — The SDK interface still accepts strings; feed them ToString() directly
using (var dv = ua.Read(n2.ToString()))
Console.WriteLine(dv.Value);

// 5) Comparison / Dictionary key (record struct automatically equals / GetHashCode)
var dict = new Dictionary<OpcUaNodeId, string>();
dict[n2] = "锅炉一温度";

// 6) Null Sentinel
if (n2 == OpcUaNodeId.Null) Console.WriteLine("空");
Console.WriteLine($"IsNull={OpcUaNodeId.Null.IsNull}");
Console.WriteLine($"n1={n1} n3={n3} n4={n4}");

String Encoding Rules​

CodingNote
i=NNNNumeric type; the ns= prefix is omitted when ns=0
ns=N;i=NNNNumeric, ns≠0
ns=N;s=XXXString type XXX, UTF-8
ns=N;g=GUIDGUID, "D" Format (00000000-0000-0000-0000-000000000000), Without Braces
ns=N;b=base64Byte array, standard Base64 (including = padding)

ToString() Output as shown in the table above; Parse() More lenient — g= The outermost { } will be automatically stripped; the expression can be parsed with or without it.ns=0 When the prefix ns=0; is omitted ToString().

Best Practices​

  • Storing Strings in Configuration Files — For persistence ToString(), for loading Parse()
  • Use strong typing within the code — In scenarios such as dictionary key/equals comparisons, strong typing is safer
  • Don't concatenate strings — "ns=" + ns + ";s=" + id Prone to errors; use FromString(ns, id).ToString()
  • TryParse comes before Parse — Always use TryParse for user input first; write fewer exception handlers
  • Identifier is an object — IdType Check the type using before retrieving the value (Identifier is uint u / is string str); direct type casting may throw an exception InvalidCastException

Cross-Language Comparison​

C#PythonJavaC++RustC
OpcUaNodeIdOpcUaNodeIdOpcUaNodeIdNodeIdOpcUaNodeIdDarraUa_OwnedNodeId
ParseparseparseParseparseDarraUa_NodeId_Parse
FromNumericfrom_numericfromNumericFromNumericfrom_numericDarraUa_NodeId_FromNumeric
FromStringfrom_stringfromStringFromStringfrom_stringDarraUa_NodeId_FromString

Next Step​