robthomas09
asked on
Reading Attribute Values from XML file in VB.NET
Hello Experts,
I am working on a VB.NET Console App in Visual Studio 2012 and i need a little help with reading the attribute values of an element in an XML file. I am working with the XML file that is attached.
Essentially my XML file looks like this. And i would like to pull the value of the Name attribute from all of the SubCategory elements inside of the XML file.
Once i have the list of attributes i am looking to hopefully pull them all into an array or something similar so that i can use the following code to create new directories for each one of the values.
I am working on a VB.NET Console App in Visual Studio 2012 and i need a little help with reading the attribute values of an element in an XML file. I am working with the XML file that is attached.
Essentially my XML file looks like this. And i would like to pull the value of the Name attribute from all of the SubCategory elements inside of the XML file.
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<MedicalRecords xmlns="Things">
<Record Archived="false" Locked="false" xmlns="">
<Label>Lab Report 21</Label>
<Location>Patients/00dmmnhq/Lab%20Report%2021.tif</Location>
<Descriptors>
<Category Name="Item">
<SubCategory Name="Lab Report"/>
</Category>
<Format>image/x-tiff</Format>
<Keywords/>
</Descriptors>
<AddedBy>
<User Number="10">
<Label>Sparks, Jeanne (jsp) 10</Label>
<Location>Users/Sparks%2c%20Jeanne%20%28jsp%29%2010/Identity.xml</Location>
</User>
<DateAndTime>2005-09-30T13:11:08.578-04:00</DateAndTime>
</AddedBy>
<ReviewedBy>
<User Number="10">
<Label>Sparks, Jeanne (jsp) 10</Label>
<Location>Users/Sparks%2c%20Jeanne%20%28jsp%29%2010/Identity.xml</Location>
</User>
<DateAndTime>2005-09-30T13:11:08.578-04:00</DateAndTime>
</ReviewedBy>
<TranscribedBy>
<User Number="10">
<Label>Sparks, Jeanne (jsp) 10</Label>
<Location>Users/Sparks%2c%20Jeanne%20%28jsp%29%2010/Identity.xml</Location>
</User>
<DateAndTime>2005-09-30T13:11:08.578-04:00</DateAndTime>
</TranscribedBy>
<Rights>Copyright 2005 by Haematology-Oncology Associates of Ohio and Michigan, P.C.</Rights>
</Record>
Once i have the list of attributes i am looking to hopefully pull them all into an array or something similar so that i can use the following code to create new directories for each one of the values.
If(Not System.IO.Directory.Exists(YourPath)) Then
System.IO.Directory.CreateDirectory(YourPath)
End If
ASKER
Hi Goran,
I'm trying the code you suggested inside of the main module here.
Unfortunately when i go to build the code i am getting an "Object reference not set to an instance of an object" on the following line
Am I missing a piece of code when i go to test it out?
I'm trying the code you suggested inside of the main module here.
Imports System
Imports System.Text
Imports System.Xml
Module Module1
Sub Main()
Dim doc As XDocument = XDocument.Load("Records.xml")
Dim names As List(Of String) = doc.Root.Elements("Record").Select(Function(n) n.Element("Descriptors").Element("Category").Element("SubCategory").Attribute("Name").Value).ToList()
Dim value As String = String.Join(",", names)
Console.WriteLine(value)
End Sub
End Module
Unfortunately when i go to build the code i am getting an "Object reference not set to an instance of an object" on the following line
Dim names As List(Of String) = doc.Root.Elements("Record").Select(Function(n) n.Element("Descriptors").Element("Category").Element("SubCategory").Attribute("Name").Value).ToList()
Am I missing a piece of code when i go to test it out?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window