How to load value of GOLD from file DefaultValues.xml ?

Ahmed Abd El Aziz 315 Reputation points
2023-06-30T00:34:49.6366667+00:00

I work on asp.net core 6 with razor page i face issue i can't get value of GOLD from xml file DefaultValues.xml

so How to get it

DeafaultValues.xml

<?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>

so on startup or program.cs how to load file

nd on razor page loadcolor.cs and loadcolor.cs.html how to load value of GOLD

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author
Anonymous
2023-06-30T03:16:33.54+00:00

Hi @Ahmed Abd El Aziz

so on startup or program.cs how to load file nd on razor page loadcolor.cs and loadcolor.cs.html how to load value of GOLD

You can use the following methods:

Note: The following sample is an Asp.net 7 Razor page application and the XML file is in the wwwroot/files folder.

1.Using IConfiguration

You can use the XML configuration provider to load the configuration from the xml file, then access the value via the IConfiguration.

Code like this: In Program.cs file:

builder.Configuration.AddXmlFile(Path.Combine(builder.Environment.WebRootPath, "files", "DeafaultValues.xml"), optional: true,
        reloadOnChange: true);


var gold = builder.Configuration["configuration:GOLD"];

In the Razor page Index.cshtml.cs file:

    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IConfiguration _configuration;

        public IndexModel(ILogger<IndexModel> logger,
            IWebHostEnvironment webHostEnvironment,
            IConfiguration configuration)
        {
            _logger = logger;
            _webHostEnvironment=webHostEnvironment;
            _configuration=configuration;
        }
        [BindProperty]
        public string Gold { get; set; }

        [BindProperty]
        public string Gold2 { get; set; }
        public void OnGet()
        {
            Gold = _configuration["configuration:GOLD"];  //get value via IConfigration.
        }
    }

2.Using LINQ To XML.

In the Program.cs file, we can use the following code:

//required using System.Xml.XPath;
XDocument po = XDocument.Load(Path.Combine(builder.Environment.WebRootPath, "files", "DeafaultValues.xml"));
// LINQ to XML query
XElement gold2 = po.Root.XPathSelectElement("configuration//GOLD");
var goldvalue = gold2.Value;

Then in the Index.cshtml.cs page, we can use the code as below:

    //required using System.Xml.XPath;
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IConfiguration _configuration;

        public IndexModel(ILogger<IndexModel> logger,
            IWebHostEnvironment webHostEnvironment,
            IConfiguration configuration)
        {
            _logger = logger;
            _webHostEnvironment=webHostEnvironment;
            _configuration=configuration;
        }
        [BindProperty]
        public string Gold { get; set; }

        [BindProperty]
        public string Gold2 { get; set; }
        public void OnGet()
        {
            Gold = _configuration["configuration:GOLD"]; //get value via Iconfiguration.


            XDocument po = XDocument.Load(Path.Combine(_webHostEnvironment.WebRootPath, "files", "DeafaultValues.xml"));

            // LINQ to XML query
            XElement gold = po.Root.XPathSelectElement("configuration//GOLD");

            Gold2 = gold.Value;
        }

The result as below:

image2


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,

Dillion

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 6,106 Reputation points
    2023-06-30T00:49:50.8233333+00:00

    Can the following Microsoft document help?

    How to find descendant elements (LINQ to XML)

    Was this answer helpful?

    0 comments No comments

Your answer

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