How to read an XML that is inside the project?

Asked

Viewed 199 times

4

I’m creating a web application with .NET Core Razor and I need to get my code to read a file XML. I can already get him to read this XML, but with another path, which is out of my project. That is, if someone else is going to execute my code, you will need to have the same path as the XML which I am using, which is not practical. If I can get it read within the project itself, the problem is solved.

Well, I’m doing it this way:

var xml = System.IO.File.ReadAllText(@"C:\Dados\VS\MenuXML\menuV11.Xml");

It’s working. But this isn’t the way I’m looking for.

My project has this structure:

inserir a descrição da imagem aqui

So I’d like him to read from this XML I left open, on the path that would be

wwwroot > Menuxml > menuV11.Xml

I tried it in the following ways:

var xml = System.IO.File.ReadAllText("~/MenuXML/menuV11");
var xml = System.IO.File.ReadAllText(@"~/MenuXML/menuV11");
var xml = System.IO.File.ReadAllText("~/MenuXML/menuV11.Xml");
var xml = System.IO.File.ReadAllText(@"~/MenuXML/menuV11.Xml");

But I did not get good results. They all gave error. I count on your help. Thank you.

  • @Maniero, what’s the problem with the question? I made it clear that I want to read the xml file and I have shown the way it is. I just don’t know how to do.

  • I think we missed the "wwwroot" var xml = System.IO.File.ReadAllText(@"wwwroot\MenuXML\menuV11.xml");

  • @Barbetta, on my layout page, I use ~ to reference the wwwroot. It worked what you gave me, but you know why it didn’t work here with the ~?

  • Never stopped to think about the reason rsrs, I will research, if I find something I warn you.

  • https://docs.microsoft.com/pt-br/aspnet/corefundamentals/static-files?view=aspnetcore-2.2. Interesting reading on the subject.

  • The original version didn’t have all that

Show 1 more comment

1 answer

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.