question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ commented

Import and Read XML and CSV file Data in Xamarin Forms

Hi How to import XML and CSV file in Xamarin forms and read Data in List. I have attached xml file screenshot. ID, Product, Amount mapping column name. ![78026-image.png][1] [1]: /answers/storage/attachments/78026-image.png

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered $$ANON_USER$$ commented

Hello,​

Welcome to our Microsoft Q&A platform!

To read data from the .csv file, try using the 'CsvHelper' nuget package. And for the xml file, try using the XmlSerializer class to get the data.
Check the link: https://stackoverflow.com/questions/9619324/how-to-read-xml-file-into-list

You could place the files in the shared project and set the Build Action to Embedded Resource. Then load the files to access the data:

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(CurrentClass)).Assembly;
Stream stream = assembly.GetManifestResourceStream("TestApplication.temp.csv");
using (var reader = new System.IO.StreamReader(stream))
{
    if (reader != null)
    {
        using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))
        {
            while (csvReader.Read())
            {
                list.Add(new TestModel
                {
                    costent = csvReader.GetField<string>(0),
                    ...
                });
            }
        }
    }
}

Tutorial: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows#loading-files-embedded-as-resources

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JarvanZhang-MSFT I am using Xamarin Essential File Picker to pick file then read and display in collection view. In mobile app i have added one import button then import .csv file for read. ![78129-image.png][1] [1]: /answers/storage/temp/78129-image.png when click on import button then read .csv file data and display on collection view.

0 Votes 0 ·

Hi @JarvanZhang-MSFT I am manually import .csv file and then read and display all data in collection view. I am using Xamarin Essential file picker function to pick file but i am unable to read data. ![78146-image.png][1] [1]: /answers/storage/temp/78146-image.png

0 Votes 0 ·

The GetManifestResourceStream methd is used for the embedded file. For the picked file, try getting the file stream using the FileResult.OpenReadAsync command. Check the code:

private async void Button_Clicked(object sender, EventArgs e)
{
    var file = await FilePicker.PickAsync();
    var stream = await file.OpenReadAsync();
    using (var reader = new System.IO.StreamReader(stream))
    {
        if (reader != null)
        {
            using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))
            {
                while (csvReader.Read())
                {
                    list.Add(new TestModel
                    {
                        ID = csvReader.GetField<string>(0),
                        content = csvReader.GetField<string>(1)
                    });
                }
            }
        }
    }

    listview.ItemsSource = list;
}
1 Vote 1 ·

Hi @JarvanZhang-MSFT

thanks this is working now.

0 Votes 0 ·