0

How can I bind below simple .xml to gridview/listview in uwp?

<TOOLS>

  <TOOL>
    <ID>0001</ID>
    <CATEGORY>PIPING</CATEGORY>
    <NAME>Straight Pipe Under internal Pressure (ASME B31.3 par.304.1.2).</NAME>
    <HINT>HOOP STRESS, CIRCUMFERENTIAL STRESS</HINT>
  </TOOL>

  <TOOL>
    <ID>0002</ID>
    <CATEGORY>PIPING</CATEGORY>
    <NAME>Mitered Segments of Pipe (ASME B31.3 par.304.2.3).</NAME>
    <HINT></HINT>
  </TOOL>

</TOOLS>

Thank you very much

2 Answers2

0

While it is not possible to bind a XML file directly, you can first deserialize this XML to plain classes which you can then bind as normal data. Simple class like:

class Tool
{
   public int Id {get;set;}
   public string Category {get;set;}
   public string Name {get;set;}
}

Should be all you need. You can deserialize the file by manually going through the XML document via one of the many approaches .NET offers, or you can deserialize automatically using attributes as seen in this SO answer.

In the end you will end up with a collection of instances of Tool which you will bind to the ItemsSource of your control:

<GridView ItemsSource="{x:Bind Data, Mode=OneWay}" />

To display it properly, you will then also need to use ItemTemplate. More info on this. There are also many tutorials on this, for example here.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
0

I would put results into a datatable using xml linq and then bind to table

Imports System.Xml
Imports System.Xml.Linq
Imports System.Data

Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim dt As New DataTable
        dt.Columns.Add("ID", GetType(String))
        dt.Columns.Add("CATEGORY", GetType(String))
        dt.Columns.Add("NAME", GetType(String))
        dt.Columns.Add("HINT", GetType(String))

        Dim doc As XDocument = XDocument.Load(FILENAME)

        For Each tool In doc.Descendants("TOOL")
            dt.Rows.Add(New Object() {tool.Element("ID").Value, tool.Element("CATEGORY").Value, tool.Element("NAME").Value, tool.Element("HINT").Value})
        Next tool

    End Sub

End Module
jdweng
  • 33,250
  • 2
  • 15
  • 20