Ad Code

Responsive Advertisement

Console App Task 10: How to store the extracted XML values into a DTO (Data Transfer Object) in C#

Console App Task 10: How to store the extracted XML values into a DTO (Data Transfer Object) in C# 

To store the extracted XML values into a DTO (Data Transfer Object), follow these steps:


1. Create a DTO Class

public class TestSummaryBKPSettingsDTO
{
    public string MonthDataDuration { get; set; }
    public string OldDataDuration { get; set; }
    public string DataTransferLogsPath { get; set; }
    public string SourcePath { get; set; }
    public string ThreeMonthDataPath { get; set; }
    public string OldDataPath { get; set; }
}

2. Read XML and Populate DTO

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        string xmlFilePath = @"C:\path\to\xyz.xml"; // Update with actual file path

        try
        {
            // Load XML
            XDocument xmlDoc = XDocument.Load(xmlFilePath);

            // Get <Settings> elements
            var settings = xmlDoc.Descendants("TestSummaryBKPSettings").Elements("Settings");

            // Create DTO object and assign values
            TestSummaryBKPSettingsDTO dto = new TestSummaryBKPSettingsDTO
            {
                MonthDataDuration = settings.ElementAt(0).Attribute("MonthDataDuration")?.Value,
                OldDataDuration = settings.ElementAt(1).Attribute("OldDataDuration")?.Value,
                DataTransferLogsPath = settings.ElementAt(2).Attribute("DataTransferLogsPath")?.Value,
                SourcePath = settings.ElementAt(2).Attribute("SourcePath")?.Value,
                ThreeMonthDataPath = settings.ElementAt(2).Attribute("ThreeMonthDataPath")?.Value,
                OldDataPath = settings.ElementAt(2).Attribute("Old_DataPath")?.Value
            };

            // Print DTO data
            Console.WriteLine($"MonthDataDuration: {dto.MonthDataDuration}");
            Console.WriteLine($"OldDataDuration: {dto.OldDataDuration}");
            Console.WriteLine($"DataTransferLogsPath: {dto.DataTransferLogsPath}");
            Console.WriteLine($"SourcePath: {dto.SourcePath}");
            Console.WriteLine($"ThreeMonthDataPath: {dto.ThreeMonthDataPath}");
            Console.WriteLine($"OldDataPath: {dto.OldDataPath}");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}

Explanation

  1. Create DTO ClassTestSummaryBKPSettingsDTO to store XML values.
  2. Load XML FileXDocument.Load(xmlFilePath).
  3. Extract Values & Assign to DTO.
  4. Print DTO Properties to verify data.

Example Output

MonthDataDuration: 3
OldDataDuration: 6
DataTransferLogsPath: Output\Log\DataTransferLogs
SourcePath: E:\User\Test Result
ThreeMonthDataPath: E:\User\Three_Month_Test_Result_Data
OldDataPath: E:\User\Old_Test_Result_Data

Now the XML data is mapped into the DTO! If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement