How can I upload the data from xml files to Azure storage account?

HonConfiance 20 Reputation points
2023-04-26T17:22:47.9233333+00:00

I want to save the data from a controller to the cloud using OPC UA. therefore, I have generated an XML file that contains the completer structure of the data to send. The problem now is to know how to load this structure in a memory from Azure and write it with OPC UA.
thanks

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
573 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,855 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,189 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RevelinoB 2,780 Reputation points
    2023-04-27T06:32:03.9+00:00

    Hi HonConfiance, To load the XML structure from Azure into memory and write it with OPC UA, you can follow these steps:

    Store the XML file in Azure: Upload the XML file to an Azure storage service, such as Azure Blob Storage, to securely store and retrieve it when needed.

    Retrieve the XML file from Azure: Use the appropriate SDK or programming language to retrieve the XML file from Azure Blob Storage. Authenticate with Azure and access the storage account where the XML file is stored. Retrieve the file as a byte stream or string.

    Parse the XML data: Once you have retrieved the XML file, parse the XML data using an XML parsing library in your preferred programming language. This will help you convert the XML into a structured format that can be easily manipulated and loaded into memory.

    Map the XML structure to OPC UA variables: After parsing the XML data, map its structure to OPC UA variables or nodes. Create OPC UA variables with the appropriate data types and hierarchy that match the structure defined in the XML. Use the available APIs or methods provided by your OPC UA SDK or library to create the variables and define their properties.

    Write OPC UA variables: With the XML structure mapped to OPC UA variables, you can now write the data from the variables to the OPC UA server. Utilize OPC UA communication methods, such as the Write service or directly setting values on the OPC UA variables, to accomplish this.

    It's important be aware of the fast that the exact implementation details may vary depending on the OPC UA SDK or library you are using and the programming language you prefer. Consult the documentation and examples provided with your chosen OPC UA SDK for specific instructions and code samples on loading the XML structure, creating OPC UA variables, and writing data to an OPC UA server hosted in Azure.

    I hope this helps?

    0 comments No comments

  2. Sumarigo-MSFT 46,206 Reputation points Microsoft Employee
    2023-05-01T05:35:08.4266667+00:00

    @HonConfiance Welcome to Microsoft Q&A Forum, Thank you for posting your

    To upload XML files to an Azure Storage account, you can use the Azure Blob Storage client library for .NET. Here's an example code snippet in C# that shows how to upload an XML file to an Azure Blob Storage container:

    using Azure.Storage.Blobs;
    using System.IO;
    // Get a connection string to your Azure Storage account
    string connectionString = "<your_connection_string>";
    // Get a reference to the blob container where you want to upload the file
    string containerName = "<your_container_name>";
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    // Upload the XML file to Azure Blob Storage
    string fileName = "<your_file_name>.xml";
    string filePath = "<path_to_your_file>.xml";
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    using FileStream stream = new FileStream(filePath, FileMode.Open);
    blobClient.Upload(stream);
    

    Replace <your_connection_string>, <your_container_name>, <your_file_name>, and <path_to_your_file> with the appropriate values for your Azure Storage account, container, and file. This code will upload the XML file located at <path_to_your_file> to Azure Blob Storage.

    To load the XML data from Azure Blob Storage into memory, you can use the DownloadTo method of the BlobClient class. Here's an example code snippet that shows how to download the XML data from Azure Blob Storage into a MemoryStream object:

    using Azure.Storage.Blobs;
    using System.IO;
    // Get a connection string to your Azure Storage account
    string connectionString = "<your_connection_string>";
    // Get a reference to the blob container where the file is stored
    string containerName = "<your_container_name>";
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    // Get a reference to the XML file
    string fileName = "<your_file_name>.xml";
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    // Download the XML data into a MemoryStream object
    MemoryStream memoryStream = new MemoryStream();
    blobClient.DownloadTo(memoryStream);
    
    

    Replace <your_connection_string>, <your_container_name>, and <your_file_name> with the appropriate values for your Azure Storage account, container, and file. This code will download the XML data from the file located at <your_file_name> in Azure Blob Storage into a MemoryStream object named memoryStream.

    Once you have the XML data loaded into memory, you can parse it using an XML parser such as XmlDocument, XmlReader, or XDocument. Once you have parsed the XML data into a structured format, you can then use an OPC UA client library to write the data to your OPC UA server.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.