Not clear what reading an xml file has to do with app.setting. Probably linq to xml would be the easiest
https://learn.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I work on asp.net core 7 razor page model . I need to use config file appsettings.json to get value of <barcode> tag
inside barcode i will get tag <nrprinter> and inside tag <nrprinter> I will get text inside <para> tag
so file DefaultValues.xml content as below :
<barcode>
<whprinter>
<para>^XA~TA000~JSN^LT0^MNM^MTD^PON^PMN^LH0,0^JMA^PR2,2^MD18^JUS^LRN^CI0^XZ</para>
<para>^XA</para>
<para>^MMT</para>
<para>^LL0184</para>
<para>^PW320</para>
<para>^LS0</para>
<para>^PA1,1,1,1</para>
<para>^JUS</para>
<para>^BY1,3,58^FT250,17^BCI,,N,N</para>
<para>^FD>:VARbarcode^FS</para>
<para>^FT220,1^A0I,20,20^FH\^FDVARFont^FS</para>
<para>^PQVARQty,0,1,Y^XZ</para>
<para/>
</whprinter>
<nrprinter>
<para>^XA~TA000~JSN^LT0^MNM^MTD^PON^PMN^LH0,0^JMA^PR2,2^MD18^JUS^LRN^CI0^XZ</para>
<para>^XA</para>
<para>^MMT</para>
<para>^LL0184</para>
<para>^PW320</para>
<para>^LS0</para>
<para>^PA1,1,1,1</para>
<para>^JUS</para>
<para>^FT249,88^A0I,25,24^FH\^FDPRICE: VARPrice^FS</para>
<para>^BY1,3,58^FT250,17^BCI,,N,N</para>
<para>^FD>:VARbarcode^FS</para>
<para>^FT220,1^A0I,20,20^FH\^FDVARFont^FS</para>
<para>^PQVARQty,0,1,Y^XZ</para>
<para/>
</nrprinter>
</barcode>
and expected result i need to get is :
^XA~TA000~JSN^LT0^MNM^MTD^PON^PMN^LH0,0^JMA^PR2,2^MD18^JUS^LRN^CI0^XZ^XA^MMT^LL0184^PW320^LS0^PA1,1,1,1^JUS^FT249,88^A0I,25,24^FH\^FDPRICE: VARPrice^FS^BY1,3,58^FT250,17^BCI,,N,N^FD>:VARbarcode^FS^FT220,1^A0I,20,20^FH\^FDVARFont^FS^PQVARQty,0,1,Y^XZ
so please how to define key and value on appsettings.json and how to get value of text inside barcode-nprinter-para by csharp ?
Not clear what reading an xml file has to do with app.setting. Probably linq to xml would be the easiest
https://learn.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview
If you wanted to store the path in appsettings.json
then it should look like this:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"XmlFilePath": "/barcode/nrprinter/para"
}
Note the XmlFilePath
node.
Then for example you could inject the IConfiguration
into a route in order to retrieve it, and then use something like XmlDocument
to retrieve all nodes with that xpath:
using System.Xml;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", (IConfiguration configuration) => {
using var fs = new FileStream("./Test.xml", FileMode.Open, FileAccess.Read);
var doc = new XmlDocument();
doc.Load(fs);
var nodes = doc.SelectNodes(configuration["XmlFilePath"]).OfType<XmlNode>();
return string.Join("", nodes.Select(n => n.InnerText));
});
app.Run();
If you run this and hit the endpoint (provided your XML file has the same filepath as the above) then you should see the result you're after returned from the "/" route.
rather than reading a config file from the razor page, I'd inject it
create a class to hold the value
public class MySettings
{
public string MyValue { get; set; } = "";
}
read and register at startup using suggested code (be sure xml file is deployed to bin folder)
...
using var fs = new FileStream("./Test.xml", FileMode.Open, FileAccess.Read);
var doc = new XmlDocument();
doc.Load(fs);
var nodes = doc.SelectNodes(configuration["XmlFilePath"]).OfType<XmlNode>();
builder.Services.AddSingleton<MySettings>(new MySettings
{
MyValue = string.Join("", nodes.Select(n => n.InnerText));
});
then inject into razor page:
@inject MySettings MySettings
<span>@MySetting.MyValue</span>
You can refer to the following sample to use LINQ to XML to find the elements and value, then display it:
appsetting.json file: add the XmlElementsPath
key value.
{
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-RazorTest-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"XmlElementsPath": "nrprinter//para"
}
The DefaultValues.xml file like this:
Then, in the Index.cshtml.cs file: use the following code to query the elements and get the value.
//required using System.Xml.Linq;
//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 Value1 { get; set; }
[BindProperty]
public string Value2 { get; set; }
public void OnGet()
{
Value1 = _configuration["XmlElementsPath"]; //get the xml elements path from appsettings.json file.
//load the xml file
XDocument po = XDocument.Load(Path.Combine(_webHostEnvironment.WebRootPath, "files", "xmlfile", "DefaultValues.xml"));
// LINQ to XML query
var valuelist = po.Root.XPathSelectElements(Value1).Select(c => c.Value).ToList();
//join the result with "/"
Value2 = string.Join("/<br/>", valuelist);
}
}
In the Index.cshtml file:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<h5>XmlElementsPath result:
@Model.Value1
</h5>
<h5>
LINQ to XML Result:
@Html.Raw(Model.Value2)
</h5>
The result as below: you can change the string.Join
method to change the separator.
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