OPC Studio User's Guide and Reference
Installed Examples - Console - UAXmlLogger

The example below logs OPC Unified Architecture data changes into an XML file.

The main program:

// Logs OPC Unified Architecture data changes into an XML file.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UAXmlLogger
{
    class Program
    {
        static void Main()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            Console.WriteLine("Starting up...");
            var xmlSerializer = new XmlSerializer(typeof(EasyUADataChangeNotificationEventArgs));
            var xmlWriter = XmlWriter.Create("UAData.xml", new XmlWriterSettings
            {
                Indent = true,
                CloseOutput = true
            });
            // The root element can have any name you need, but the name below also allows reading the log back as .NET array
            xmlWriter.WriteStartElement("ArrayOfEasyUADataChangeNotificationEventArgs");

            Console.WriteLine("Logging data for 30 seconds...");
            int handle = EasyUAClient.SharedInstance.SubscribeDataChange(endpointDescriptor, 
                "nsu=http://test.org/UA/Data/ ;i=10853", 
                samplingInterval:100,
               (_, eventArgs) =>
               {
                   Debug.Assert(!(eventArgs is null));
                   Console.Write(".");
                   xmlSerializer.Serialize(xmlWriter, eventArgs);
               });
            System.Threading.Thread.Sleep(30 * 1000);

            Console.WriteLine();
            Console.WriteLine("Shutting down...");
            EasyUAClient.SharedInstance.UnsubscribeMonitoredItem(handle);
            xmlWriter.WriteEndElement();    // not really necessary - XmlWriter would write the end tag for us anyway
            xmlWriter.Close();

            Console.WriteLine("Finished.");
        }
    }
}

 

See Also

Examples - OPC Unified Architecture

Conceptual