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
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
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),
...
});
}
}
}
}
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.
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.
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
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;
}
6 people are following this question.