Search code examples
jqueryasp.net-mvcasp.net-corerazorrazor-pages

How to load value of GOLD from xml file?


I work on asp.net core 6 razor view . I face issue I can't get value of GOLD 003 on asp.net core 6 application

Name of file DfaultValues.xml

and content as below

<?xml version="1.0" encoding="utf-8" ?>
<defaultvalues>
    
        
    <UserDetails>
        <UserID>9595</UserID>
        <UserName>Aweer User</UserName>
        <UserType>PY</UserType>
        <Branch>A</Branch>
        <BranchCode>10206</BranchCode>
        <BranchDesc></BranchDesc>
        <BranchType>B</BranchType>
        <BusinessUnit>Jumeira</BusinessUnit>
        <CompanyNo>00001</CompanyNo>
        <CompanyName>Union Co-Operative Society</CompanyName>
    </UserDetails>
    <configuration>
        <LPAPPID></LPAPPID>
        <LPSQLPY>101</LPSQLPY>
        <LPJDEPY>807</LPJDEPY>
        <LPSQLPD>102</LPSQLPD>
        <LPJDEPD>310</LPJDEPD>
        <PRINTERIP>172.17.0.53</PRINTERIP>
        <PRINTERNAME>Black4</PRINTERNAME>
        <TEMPCONFIG>Config.temp</TEMPCONFIG>
        <GOLD>003</GOLD>
        <SILVER>002</SILVER>
        <GREEN>01</GREEN>
        <BLACK>004</BLACK>
        <NORMAL>001</NORMAL>
    </configuration>
</defaultvalues>

my model on razor app asp.net core

public class XMLInfo
    {
      
        public string GOLD { get; set; }
       
     }

So How to load file on program.cs

and How to get value of gold on page load color

page loadcolor.html.cs

public class loadcolor : PageModel
    {
public void OnGet()
{
// how to get color Gold value 003
}
}

Solution

  • You can check the detailed steps I shared, and I can get the value of GOLD.

    Test Result

    enter image description here

    gold.cshtml

    @page "/gold"
    @model IdentityTest.Pages.goldModel
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Load Color</title>
    </head>
    <body>
        <h1>Color: @Model.GoldValue</h1>
    </body>
    </html>
    
    @{
    }
    

    gold.cshtml.cs

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using System.Xml;
    
    namespace IdentityTest.Pages
    {
        public class goldModel : PageModel
        {
            public string? GoldValue { get; set; }
            public void OnGet()
            {
                string filePath = "wwwroot/xml/goldFile.xml"; // Replace with the actual path to your XML file
    
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filePath);
    
                XmlNode goldNode = xmlDoc.SelectSingleNode("//configuration/GOLD");
                GoldValue = goldNode.InnerText;
            }
        }
    }
    

    The structure of my project contents

    enter image description here