Here is a different way of doing the same thing [in C#]:
string xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ArrayOfAssessmentItemTimed xmlns=""http://g.h.i"">
<AssessmentItemTimed>
<Auto Model=""Nissan""/>
<License>0577HJK</License>
<ChassisNumber>VF1KT1RG646667276</ChassisNumber>
<Kilometers>90957</Kilometers>
</AssessmentItemTimed>
<AssessmentItemTimed>
<License>0918HHM</License>
<ChassisNumber>VF1KT1RG646272649</ChassisNumber>
<Kilometers>158142</Kilometers>
</AssessmentItemTimed>
</ArrayOfAssessmentItemTimed>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
nsManager.AddNamespace("ns", "http://g.h.i");
XmlNodeList assessmentItems = xmlDoc.SelectNodes("//ns:AssessmentItemTimed", nsManager);
foreach (XmlNode item in assessmentItems)
{
Console.WriteLine("Assessment Item:");
foreach (XmlNode childNode in item.ChildNodes)
{
Console.WriteLine($" Element: {childNode.Name}, Value: {childNode.InnerText}");
if (childNode.Attributes != null)
{
foreach (XmlAttribute attribute in childNode.Attributes)
{
Console.WriteLine($" Name: {attribute.Name}, Value: {attribute.Value}");
}
}
}
Console.WriteLine();
}
UPDATE
Thanks to Dave for pointing out the answer needs to be in VB.Net. So here is the conversion (Thanks to ChatGpt):
Imports System
Imports System.Xml
Module Module1
Sub Main()
Dim xmlString As String = "<?xml version=""1.0"" encoding=""utf-8""?>
<ArrayOfAssessmentItemTimed xmlns=""http://g.h.i"">
<AssessmentItemTimed>
<Auto Model=""Nissan""/>
<License>0577HJK</License>
<ChassisNumber>VF1KT1RG646667276</ChassisNumber>
<Kilometers>90957</Kilometers>
</AssessmentItemTimed>
<AssessmentItemTimed>
<License>0918HHM</License>
<ChassisNumber>VF1KT1RG646272649</ChassisNumber>
<Kilometers>158142</Kilometers>
</AssessmentItemTimed>
</ArrayOfAssessmentItemTimed>"
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(xmlString)
Dim nsManager As New XmlNamespaceManager(xmlDoc.NameTable)
nsManager.AddNamespace("ns", "http://g.h.i")
Dim assessmentItems As XmlNodeList = xmlDoc.SelectNodes("//ns:AssessmentItemTimed", nsManager)
For Each item As XmlNode In assessmentItems
Console.WriteLine("Assessment Item:")
For Each childNode As XmlNode In item.ChildNodes
Console.WriteLine($" Element: {childNode.Name}, Value: {childNode.InnerText}")
If childNode.Attributes IsNot Nothing Then
For Each attribute As XmlAttribute In childNode.Attributes
Console.WriteLine($" Name: {attribute.Name}, Value: {attribute.Value}")
Next
End If
Next
Console.WriteLine()
Next
End Sub
End Module
And the output:
Assessment Item:
Element: Auto, Value:
Name: Model, Value: Nissan
Element: License, Value: 0577HJK
Element: ChassisNumber, Value: VF1KT1RG646667276
Element: Kilometers, Value: 90957
Assessment Item:
Element: License, Value: 0918HHM
Element: ChassisNumber, Value: VF1KT1RG646272649
Element: Kilometers, Value: 158142