Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm trying to read attributes of a XML node (another system)...
This is an XML of example:

XML
<?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>


I need to read "Model" and "Nissan".
I have tried with this VB code, but MsgBox is blank.

What I have tried:

VB
Imports System.IO
Imports <xmlns="http://g.h.i">

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Multiselect = False
        OpenFileDialog1.ShowDialog()
        Dim fileName As String = Path.GetFileName(OpenFileDialog1.FileName)
        Dim filePath As String = OpenFileDialog1.FileName
        Me.Text = filePath

        Dim xDoc = XDocument.Load(filePath)

        For Each x In xDoc.<ArrayOfAssessmentItemTimed>.<AssessmentItemTimed>

            MsgBox(x.<Auto>.Value) 'Here I'd like to read Model=Nissan

        Next
    End Sub
End Class
Posted
Updated 2-Oct-23 5:22am
v2

You were given the answer at How read name of attribute of XML nodes (not value)![^]. You just need to adapt that code to get the names into the MessageBox.
 
Share this answer
 
This is not that hard:
VB.NET
Dim doc As XDocument =
    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
        <body surname="Rott" name="Pig">
            <city>London</city>
        </body>
        <body surname="Lip" name="Jack">
            <city>Los Angeles</city>
        </body>
        <body colorB="White" colorA="Yellow">
            <city>Rome</city>
        </body>
    </Root>

For Each element In doc.<Root>.<body>
    Console.WriteLine(element.Name.LocalName)

    For Each attr As XAttribute In element.Attributes
        Console.WriteLine(vbTab & attr.Name.LocalName & " : " & attr.Value)
    Next
Next

The Name property of an X element cannot be directly converted to a string. You're looking for the LocalName, which is a string and can be directly output.
 
Share this answer
 
Here is a different way of doing the same thing [in C#]:
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):
VB.NET
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
 
Share this answer
 
v2
Comments
Dave Kreskowiak 24-Sep-23 23:04pm    
Pssst. The OP is in VB.NET, not C#.
Graeme_Grant 24-Sep-23 23:08pm    
There is that ... lol ... Thanks, will post an update

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900