Question Parsing XML file and importing to Excel

Onamission71

Member
Joined
Jul 8, 2009
Messages
14
Programming Experience
Beginner
I have an XML file which i am trying to parse and then import the results i want in to 3 columns in Excel, I have tried various methods, but still am struggling, please help...
The XML file looks like this:-
<?xml version="1.0"?>
-<RealTimeMetrics SiteId="Local">
-<Properties>
<MacAddress>01:b0:9d:a7:02:1d</MacAddress>
<IpAddress>192.168.1.7</IpAddress>
<Timezone>0</Timezone>
<DST>1</DST>
<DeviceType>0</DeviceType>
<SerialNumber>1589</SerialNumber>
</Properties>
-<RTReport Date="2013-01-10T21:07:12">
-<RTObject Name="Sensor1" ObjectType="0" Devicename="Door" DeviceId="Outside" Id="0">
<RTCount TotalExits="64" TotalEnters="29"/>
</RTObject>
</RTReport>
</RealTimeMetrics>

The Columns i require in Excel are the Date, Total Exits, and Total Enters.
Could somebody show me how to do this or point me in the right direction
 
In Excel 2007+ Xml can be read directly. You can just pass the file straight over to an Excel process.
If you don't want all the columns to show you could use a little LINQ to filter it down a bit.

You need to wrap your XML in a root node like this

VB.NET:
<?xml version="1.0"?>
<root>-
<RealTimeMetrics SiteId="Local">
-<Properties>
<MacAddress>01:b0:9d:a7:02:1d</MacAddress>
<IpAddress>192.168.1.7</IpAddress>
<Timezone>0</Timezone>
<DST>1</DST>
<DeviceType>0</DeviceType>
<SerialNumber>1589</SerialNumber>
</Properties>
-<RTReport Date="2013-01-10T21:07:12">
-<RTObject Name="Sensor1" ObjectType="0" Devicename="Door" DeviceId="Outside" Id="0">
<RTCount TotalExits="64" TotalEnters="29"/>
</RTObject>
</RTReport>
</RealTimeMetrics>
</root>

Then you can use LINQ

    Dim document = XDocument.Load("path_to_xml_file")
    document.Descendants("RealTimeMetrics").Elements().Where(Function(x)x.Attribute("Date") is nothing).remove()
    document.Descendants("RTObject").Attributes().remove()


This statement reduces the example Xml to this

VB.NET:
<root>-
<RealTimeMetrics  SiteId="Local">
-
-<RTReport  Date="2013-01-10T21:07:12">
-<RTObject>
<RTCount TotalExits="64"  TotalEnters="29"  />
</RTObject>
</RTReport>
</RealTimeMetrics>
</root>
 

Similar threads

Back
Top