Hi jon,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<SFA_ORDCLIENTE> listaOrdini = new List<SFA_ORDCLIENTE>();
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new System.IO.StreamReader(Server.MapPath("~/Doc.xml"))))
{
SFA_ORDCLIENTE ordine = new SFA_ORDCLIENTE();
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == System.Xml.XmlNodeType.Element)
{
if (reader.Name == "row")
{
System.Xml.Linq.XElement el = System.Xml.Linq.XNode.ReadFrom(reader) as System.Xml.Linq.XElement;
if (el != null)
{
IEnumerable<System.Xml.Linq.XElement> elements = el.Elements("field");
foreach (System.Xml.Linq.XElement element in elements)
{
var elName = element.FirstAttribute.Value;
switch (elName.ToLower())
{
case "anno":
ordine.Anno = Convert.ToInt16(element.Value);
break;
case "att":
ordine.Codatt = element.Value;
break;
case "cen":
ordine.Codcen = element.Value;
break;
default:
//ordine = null;
break;
}
}
if (ordine != null)
{
listaOrdini.Add(ordine);
}
}
}
}
}
}
}
class SFA_ORDCLIENTE
{
public int id { get; set; }
public int Anno { get; set; }
public string Codatt { get; set; }
public string Codcen { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim listaOrdini As List(Of SFA_ORDCLIENTE) = New List(Of SFA_ORDCLIENTE)()
Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New IO.StreamReader(Server.MapPath("~/Doc.xml")))
Dim ordine As SFA_ORDCLIENTE = New SFA_ORDCLIENTE()
reader.MoveToContent()
While reader.Read()
If reader.NodeType = System.Xml.XmlNodeType.Element Then
If reader.Name = "row" Then
Dim el As XElement = TryCast(XNode.ReadFrom(reader), XElement)
If el IsNot Nothing Then
Dim elements As IEnumerable(Of XElement) = el.Elements("field")
For Each element As XElement In elements
Dim elName = element.FirstAttribute.Value
Select Case elName.ToLower()
Case "anno"
ordine.Anno = Convert.ToInt16(element.Value)
Case "att"
ordine.Codatt = element.Value
Case "cen"
ordine.Codcen = element.Value
Case Else
End Select
Next
If ordine IsNot Nothing Then
listaOrdini.Add(ordine)
End If
End If
End If
End If
End While
End Using
End Sub
Class SFA_ORDCLIENTE
Public Property id As Integer
Public Property Anno As Integer
Public Property Codatt As String
Public Property Codcen As String
End Class
XML
<doc>
<table name="ordini">
<row>
<field name="id">0431524493258932</field>
<field name="anno">2018</field>
<field name="att">0000</field>
<field name="cen">01</field>
</row>
<row>
<field name="id">1041524493596749</field>
<field name="anno">2018</field>
<field name="att">0000</field>
<field name="cen">01</field>
</row>
</table>
<table name="righe">
<row>
<field name="id">0431524493258932</field>
<field name="anno">2018</field>
<field name="att">0000</field>
<field name="cen">4U</field>
</row>
</table>
</doc>
Screenshot