Articles in this section
Category / Section

How to Bind XML data into Syncfusion Blazor Listview Component?

2 mins read

To connect the XML data to the Blazor ListView component, you must convert the XML data into a List object type and then assign that data to the ListView component.

Please refer to the following code sample:

[Index.razor]

@using Syncfusion.Blazor.Lists
@using System.Xml.Linq;

<sflistview datasource="@Data">
   <listviewfieldsettings tvalue="DataModel" id="Id" text="Text"></listviewfieldsettings>
</sflistview>

@code{

public class DataModel
{
   public string Text { get; set; }
   public string Id { get; set; }
}

private List<datamodel> Data = new List<datamodel>();

protected override async Task OnInitializedAsync()
{
   await GetData();
}

// Read the XML file and extract the data
private async Task GetData()
{
   var xmlFilePath = "Pages/XMLFile.xml"; // Replace with the actual path
   var xmlContent = await File.ReadAllTextAsync(xmlFilePath);
   var xmlDoc = XDocument.Parse(xmlContent);
   Data = xmlDoc.Descendants("item")
                .Select(item => new DataModel
                    {
                        Text = item.Element("Text").Value,
                        Id = item.Element("Id").Value
                    })
                .ToList();
}

[XMLFile.xml]

<!--?xml version="1.0" encoding="utf-8" ?-->
<items>
   <item>
   	<text>ArtWork</text>
   	<id>list-01</id>
   </item>
   <item>
   	<text>Abstract</text>
   	<id>list-02</id>
   </item>
   <item>
   	<text>Modern Painting</text>
   	<id>list-03</id>
   </item>
   <item>
   	<text>Ceramics</text>
   	<id>list-04</id>
   </item>
   <item>
   	<text>Animation Art</text>
   	<id>list-05</id>
   </item>
   <item>
   	<text>Oil Painting</text>
   	<id>list-06</id>
   </item>
</items>

Refer to the following GitHub link for samples:

XML data binding in blazor listview

Conclusion
We hope you enjoyed learning about how to Bind XML Data into Syncfusion Blazor Listview Component.

You can refer to our Blazor ListView feature tour page to know about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor ListView example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor ListView and other Blazor components.

If you have any questions or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied