Read XML file In MAUI project

1 Answer 929 Views
General Discussions
Daniel
Top achievements
Rank 1
Silver
Bronze
Daniel asked on 01 Mar 2022, 03:59 PM

Hi,

Do you have example how I read XML file, where I need to put in MAUI project and how  I read it ?

Thanks in advance.

 

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 02 Mar 2022, 07:39 AM

Hi,

Thank you very much for the sample!!!

My problem is how to load this package (Microsoft.Maui.Essentials)  from NuGet Packages: manager

Which name I have to take., it not there.

This is your sample code:

 

This is my serach:

Thanks in advance,

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 02 Mar 2022, 07:45 AM

I used this :https://devblogs.microsoft.com/xamarin/xamarin-essentials-1-7-and-introducing-net-maui-essentials/

and I have <UseMaui>true</UseMaui> in the .csproj.

and add using using Microsoft.Maui.Essentials;

but is not work.

Lance | Manager Technical Support
Telerik team
commented on 02 Mar 2022, 02:33 PM

I only did a Visual Studio 2022 Preview > File > New  > MAUI project, I did not personally add any NuGet packages. Just double check that you are using the latest version of VS 2022 preview, and do a project Rebuild (not a Build). That will ensure the packages are restored from nuget.org before the code is compiled.

Regarding your link, that blog post is far too old. You want to keep in mind that .NET MAUI is a preview technology. Anything you read online right now has to be no older than 3-4 weeks, otherwise it very likely inaccurate. This is because .NET MAUI has a lot of changes every release, which is anout every 30 days. After it is officially released, then you can rely on everything being stable.

My recommendation is, during this preview period, only use the official Microsoft Documentation for MAUI

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 02 Mar 2022, 02:56 PM

I use VS 2022 as shown here, I rebuild the project and the package Microsoft.MAUI.Essentials not appear, I missed something.

Lance | Manager Technical Support
Telerik team
commented on 02 Mar 2022, 03:12 PM

Microsoft's NuGet packages do not come from Visual Studio, they come from nuget.org when you do a Rebuild or manual restore.

If you are not familiar with Visual Studio and using NuGet packages, I strongly recommend stop everything you're doing right now and follow this tutorial Install and use a NuGet package in Visual Studio | Microsoft Docs

Understanding how NuGet packages work is absolutely critical in understanding how to work with .NET projects like MAUI. That knowledge will also help you with other Visual Studio project types like ASP.NET Core, WPF and WinForms in a post-NET Framework world.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 02 Mar 2022, 03:24 PM

I opened new MAUI project and now I have this package ,but  VS don't  recognize this :

 await using var stream = await FileSystem.OpenAppPackageFileAsync("Configuration.xml");

I tried to add using using Microsoft.Maui.Essentials;

also not help.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 02 Mar 2022, 03:28 PM

What do I miss?
Lance | Manager Technical Support
Telerik team
commented on 02 Mar 2022, 04:37 PM

Hi Daniel, just use my attached project. There seems to be a problem with whatever project you're using, I can't tell you why packages are not restoring.

Also, you do not need to add a using statement (unless you've intentionally disabled global usings). What I suspect your problem is you are not restoring the packages or are not doing a Build/Rebuild.

Also, make sure the scope of the code editor in on the platform you're compiling for:

Keep in mind that MAUI is in preview and that tooling problems are expected to occur. When you encounter such issues, Microsoft asks that you report them so that they can work on it for the next preview release.

Here is how you can report a Visual Studio problem:

 

At this point, I'm afraid there isn't much Telerik Support can assist you with there. You're experiencing tooling issues that are outside the scope of what we can help with. You need the attention of the MAUI team or the Visual Studio team, open a new forum post in Microsoft Q&A so they can help you. (links in my answer)

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 07:51 AM | edited

I correct my self.

The file located in class library of dot net 5 and MAUI hosted it.

var  configurationXml = await File.ReadAllTextAsync(@"Configuration.xml");

When I read this file from volume D is ok.

But when I put the file in class library or MAUI I got excption.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 07:55 AM

The exception:

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 11:45 AM

 This the results I found :
var locationOfTheDll = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var locationOfTheXml = locationOfTheDll.Replace("Shared.dll", "Configuration.xml");
            var configurationXml = await File.ReadAllTextAsync(locationOfTheXml);
            return configurationXml;

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 12:56 PM

Can you please attach empty project with reference to TELERIK nuget call it

AutomationClient.MAUI. in net 6

The reason I have problem with my project.

Lance | Manager Technical Support
Telerik team
commented on 03 Mar 2022, 02:07 PM

Hi Daniel, that code is incorrect (it looks liek you copied it from a project that uses embedded resources). This is not how it work in .NET MAUI.

Instead, you can include the file in the project's Assets/Raw folder and make sure the file's BuildAction is set to MauiAsset.

I have no idea what the problem is you're having with general project structure, it is unrelated to Telerik UI for MAUI. For further assistance with this, I recommend working with the Microsoft, go to https://docs.microsoft.com/en-us/answers/topics/dotnet-csharp.html 

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 02:17 PM

The problem is that I read the file in Class library of net 5 and the hosted Environment of this library is MAUI.

I put the xml both in Class library of net 5 && In MAUI and I got this exception:

var configurationXml = await File.ReadAllTextAsync(@"Configuration.xml");
 and I got exception :

How is can be solved ?     

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 03 Mar 2022, 02:17 PM

I correct my self.

The xml file is located in Class library of dot net 5 and the hosted  in MAUI that used this class library.

var configurationXml = await File.ReadAllTextAsync(@"Configuration.xml");

When I read from D volume is ok but when I put  the xml in class library or MAUI

I got this exception.

Lance | Manager Technical Support
Telerik team
commented on 03 Mar 2022, 02:58 PM

Hi Daniel, the Telerik Forums cannot help you with that issue. Please open a post in the Microsoft Forums or StackOverflow using the links I have provided in my answer
Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 07 Mar 2022, 07:19 AM

The problem solved like in your demo after uninstall and install VS 2022 preview version 2


/// <summary>
    /// Get configuration file data stracture
    /// </summary>
    /// <returns></returns>
    private async Task<string> GetConfigurations()
    {
        await using var stream = await FileSystem.OpenAppPackageFileAsync("Configurationxml");
        using var reader = new StreamReader(stream);
        var configurationXml = await reader.ReadToEndAsync();
        
        return configurationXml;

    }

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 01 Mar 2022, 04:13 PM | edited on 01 Mar 2022, 10:36 PM

Hi Daniel,

Telerik UI for MAUI is a set of UI components, whereas code that reads data from files is general .NET development topic (i.e. something you can do with the .NET and is not done with a UI component).

The Telerik forums and Telerik Support Ticket system are designed for getting assistance with the UI components themselves, not general development topics. To ask for help with general .NET development topics, you can search or post in the following locations:

Microsoft Documentation

You will also find a lot of good resources in the Microsoft Documentation for C#. Since .NET MAUI uses .NET 6, you will find a significant level of compatibility with any code you find.

For example, I just did a quick Google/Bing search for "How do I load an XML file in C#" and this documentation was at the top => https://docs.microsoft.com/en-us/dotnet/standard/linq/load-xml-file. You can also find other good examples for the XmlReader docs => How to create a tree from an XmlReader - LINQ to XML | Microsoft Docs

 > Here is a nice little tutorial that shows you three different ways to load XML. I personally prefer the first option, but you'd need to have defined the classes ahead of time. If you do not want to setup classes, then I recommend option #2.

 

Demo

Attached is a demo I've written for you that contains some code to help you get started. It loads simple XML into a List<Book> . You can do whatever you want from there. For example, you could add Telerik components and then use the List with a DataGrid or anything else you want.

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Lance | Manager Technical Support
Telerik team
commented on 01 Mar 2022, 04:25 PM

Tags
General Discussions
Asked by
Daniel
Top achievements
Rank 1
Silver
Bronze
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or