abcserve
asked on
How do I extract data from an XML File?
My experience with XML is limited. I am trying to extract data from the sampling of the below XML file at end. The data use to be in a text file that was imported into Excel and processed via macros. I need to get the XML data into the same spreadsheet format to continue using the extensive programming within the Excel file.
Using the XMLTextReader, I am able to see the data, but I am trying to execute specific code based on the various elements. Example, the values of the SummaryValueData will be written in a specific area of an Excel Spreadsheet. The values of the DailyTestSummaryData will be written to another location as will the DailyCalibrationData values within the DailyTestSummaryData. The same will be true for the HourlyOperatingData. I am looking specifically for VB.Net coding (I am using 2003) to extract the data based on the major elements. Using the Excel XML features do not accomplish what I need, hence the VB.NET approach.
================CODE SNIPPET
Sub emXml()
'this section will process the EM xml file
'strFileName is a global variable
MsgBox("emXml : " & strFileName)
Dim strTemp As String
'Declare and Load file
Dim xmlDoc As New XmlDocument
Dim xNode As XmlNode
xmlDoc.Load(strFileName)
Dim reader As XmlTextReader = New XmlTextReader(strFileName)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
strTemp = (reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute ()
'Display attribute name and value.
strTemp = strTemp & (" {0}='{1}'" & reader.Name & reader.Value)
End While
End If
strTemp = strTemp
If strTemp = "SummaryValueData" Then
'this returns an integer...not what I wanted
MsgBox(XmlNodeType.Text & " " & reader.Value)
End If
If strTemp = "DailyTestSummaryData" Then
'this returns an integer...not what I wanted
MsgBox("DTSD " & XmlNodeType.Text & " " & reader.Value)
End If
Case XmlNodeType.Text 'Display the text in each element.
'this displays all of my values
lblFileName.Text = strTemp & ": " & reader.Value()
lblFileName.Refresh()
Dim x As Integer
For x = 1 To 100000000
'for next set just to allow the lblFileName.text
'to be read-not to be left as permanent part
'of program
Next
Select Case strTemp
'this is looking at various element names and gives appropriate value except it
'never hits the SummaryValueData Case
Case "ORISCode"
MsgBox("ORis is " & reader.Value)
Case "Year"
MsgBox("Year is " & reader.Value)
Case "SummaryValueData"
MsgBox("SummaryValueData is " & reader.Value)
End Select
End Select
Loop
'Call UpdateExcel()
End Sub
''''''''''XML FILE SAMPLING'''''''''''''''''' ''''
<?xml version="1.0" encoding="utf-8"?>
<Emissions>
<ORISCode>1234</ORISCode>
<Year>2007</Year>
<Quarter>1</Quarter>
<SubmissionComment/>
<Version>1.2</Version>
<SummaryValueData>
<UnitID>001</UnitID>
<ParameterCode>CO2M</Param eterCode>
<CurrentReportingPeriodTot al>238270. 800</Curre ntReportin gPeriodTot al>
<OzoneSeasonToDateTotal/>
<YearToDateTotal>238270.80 0</YearToD ateTotal>
</SummaryValueData>
<DailyTestSummaryData>
<UnitID>001</UnitID>
<Date>2009-01-01</Date>
<Hour>7</Hour>
<Minute>24</Minute>
<MonitoringSystemID/>
<ComponentID>013</Componen tID>
<TestTypeCode>DAYCAL</Test TypeCode>
<TestResultCode>PASSED</Te stResultCo de>
<SpanScaleCode/>
<DailyCalibrationData>
<OnLineOffLineIndicator>1< /OnLineOff LineIndica tor>
<UpscaleGasCode>HIGH</Upsc aleGasCode >
<ZeroInjectionDate>2009-01 -01</ZeroI njectionDa te>
<ZeroInjectionHour>7</Zero InjectionH our>
<ZeroInjectionMinute>22</Z eroInjecti onMinute>
<UpscaleInjectionDate>2009 -01-01</Up scaleInjec tionDate>
<UpscaleInjectionHour>7</U pscaleInje ctionHour>
<UpscaleInjectionMinute>24 </UpscaleI njectionMi nute>
<ZeroMeasuredValue>0.000</ ZeroMeasur edValue>
<UpscaleMeasuredValue>2.60 0</Upscale MeasuredVa lue>
<ZeroAPSIndicator>0</ZeroA PSIndicato r>
<UpscaleAPSIndicator>0</Up scaleAPSIn dicator>
<ZeroCalibrationError>0.00 </ZeroCali brationErr or>
<UpscaleCalibrationError>2 .50</Upsca leCalibrat ionError>
<ZeroReferenceValue>0.000< /ZeroRefer enceValue>
<UpscaleReferenceValue>2.5 00</Upscal eReference Value>
</DailyCalibrationData>
</DailyTestSummaryData>
<DailyTestSummaryData>
<UnitID>001</UnitID>
<Date>2009-01-01</Date>
<Hour>7</Hour>
<Minute>25</Minute>
<MonitoringSystemID/>
<ComponentID>013</Componen tID>
<TestTypeCode>INTCHK</Test TypeCode>
<TestResultCode>PASSED</Te stResultCo de>
<SpanScaleCode/>
</DailyTestSummaryData>
<HourlyOperatingData>
<UnitID>001</UnitID>
<Date>2009-03-20</Date>
<Hour>15</Hour>
<OperatingTime>1.00</Opera tingTime>
<HourLoad>90</HourLoad>
<LoadUnitsOfMeasureCode>MW </LoadUnit sOfMeasure Code>
<LoadRange>9</LoadRange>
<CommonStackLoadRange/>
<FcFactor>1840.0</FcFactor >
<FdFactor/>
<FwFactor/>
<FuelCode/>
<MonitorHourlyValueData>
<ParameterCode>CO2C</Param eterCode>
<UnadjustedHourlyValue>12. 700</Unadj ustedHourl yValue>
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID>104</M onitoringS ystemID>
<ComponentID>014</Componen tID>
<PercentAvailable>86.8</Pe rcentAvail able>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>FLOW</Param eterCode>
<UnadjustedHourlyValue>151 77000.000< /Unadjuste dHourlyVal ue>
<AdjustedHourlyValue>15420 000.000</A djustedHou rlyValue>
<MODCCode>01</MODCCode>
<MonitoringSystemID>103</M onitoringS ystemID>
<ComponentID>013</Componen tID>
<PercentAvailable>99.9</Pe rcentAvail able>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>NOXC</Param eterCode>
<UnadjustedHourlyValue>107 .900</Unad justedHour lyValue>
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID/>
<ComponentID>015</Componen tID>
<PercentAvailable/>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>SO2C</Param eterCode>
<UnadjustedHourlyValue>51. 800</Unadj ustedHourl yValue>
<AdjustedHourlyValue>51.80 0</Adjuste dHourlyVal ue>
<MODCCode>01</MODCCode>
<MonitoringSystemID>102</M onitoringS ystemID>
<ComponentID>012</Componen tID>
<PercentAvailable>86.8</Pe rcentAvail able>
<MoistureBasis/>
</MonitorHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>CO2</Parame terCode>
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>111.6 000</Adjus tedHourlyV alue>
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>401</Fo rmulaIdent ifier>
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>HI</Paramet erCode>
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>1064. 3000</Adju stedHourly Value>
<MODCCode/>
<MonitoringSystemID>104</M onitoringS ystemID>
<FormulaIdentifier>801</Fo rmulaIdent ifier>
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>NOXR</Param eterCode>
<UnadjustedHourlyValue>0.1 87</Unadju stedHourly Value>
<AdjustedHourlyValue>0.187 0</Adjuste dHourlyVal ue>
<MODCCode>01</MODCCode>
<MonitoringSystemID>105</M onitoringS ystemID>
<FormulaIdentifier>501</Fo rmulaIdent ifier>
<PercentAvailable>78.4</Pe rcentAvail able>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>SO2</Parame terCode>
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>132.6 000</Adjus tedHourlyV alue>
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>201</Fo rmulaIdent ifier>
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
</HourlyOperatingData>
<HourlyOperatingData>
<UnitID>001</UnitID>
<Date>2009-03-31</Date>
<Hour>23</Hour>
<OperatingTime>1.00</Opera tingTime>
<HourLoad>89</HourLoad>
<LoadUnitsOfMeasureCode>MW </LoadUnit sOfMeasure Code>
<LoadRange>9</LoadRange>
<CommonStackLoadRange/>
<FcFactor>1840.0</FcFactor >
<FdFactor/>
<FwFactor/>
<FuelCode/>
<MonitorHourlyValueData>
<ParameterCode>NOXC</Param eterCode>
<UnadjustedHourlyValue>108 .800</Unad justedHour lyValue>
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID/>
<ComponentID>015</Componen tID>
<PercentAvailable/>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>SO2C</Param eterCode>
<UnadjustedHourlyValue>57. 000</Unadj ustedHourl yValue>
<AdjustedHourlyValue>57.00 0</Adjuste dHourlyVal ue>
<MODCCode>01</MODCCode>
<MonitoringSystemID>102</M onitoringS ystemID>
<ComponentID>012</Componen tID>
<PercentAvailable>86.8</Pe rcentAvail able>
<MoistureBasis/>
</MonitorHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>SO2</Parame terCode>
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>144.2 000</Adjus tedHourlyV alue>
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>201</Fo rmulaIdent ifier>
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
</HourlyOperatingData>
</Emissions>
Using the XMLTextReader, I am able to see the data, but I am trying to execute specific code based on the various elements. Example, the values of the SummaryValueData will be written in a specific area of an Excel Spreadsheet. The values of the DailyTestSummaryData will be written to another location as will the DailyCalibrationData values within the DailyTestSummaryData. The same will be true for the HourlyOperatingData. I am looking specifically for VB.Net coding (I am using 2003) to extract the data based on the major elements. Using the Excel XML features do not accomplish what I need, hence the VB.NET approach.
================CODE SNIPPET
Sub emXml()
'this section will process the EM xml file
'strFileName is a global variable
MsgBox("emXml : " & strFileName)
Dim strTemp As String
'Declare and Load file
Dim xmlDoc As New XmlDocument
Dim xNode As XmlNode
xmlDoc.Load(strFileName)
Dim reader As XmlTextReader = New XmlTextReader(strFileName)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
strTemp = (reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute
'Display attribute name and value.
strTemp = strTemp & (" {0}='{1}'" & reader.Name & reader.Value)
End While
End If
strTemp = strTemp
If strTemp = "SummaryValueData" Then
'this returns an integer...not what I wanted
MsgBox(XmlNodeType.Text & " " & reader.Value)
End If
If strTemp = "DailyTestSummaryData" Then
'this returns an integer...not what I wanted
MsgBox("DTSD " & XmlNodeType.Text & " " & reader.Value)
End If
Case XmlNodeType.Text 'Display the text in each element.
'this displays all of my values
lblFileName.Text = strTemp & ": " & reader.Value()
lblFileName.Refresh()
Dim x As Integer
For x = 1 To 100000000
'for next set just to allow the lblFileName.text
'to be read-not to be left as permanent part
'of program
Next
Select Case strTemp
'this is looking at various element names and gives appropriate value except it
'never hits the SummaryValueData Case
Case "ORISCode"
MsgBox("ORis is " & reader.Value)
Case "Year"
MsgBox("Year is " & reader.Value)
Case "SummaryValueData"
MsgBox("SummaryValueData is " & reader.Value)
End Select
End Select
Loop
'Call UpdateExcel()
End Sub
''''''''''XML FILE SAMPLING''''''''''''''''''
<?xml version="1.0" encoding="utf-8"?>
<Emissions>
<ORISCode>1234</ORISCode>
<Year>2007</Year>
<Quarter>1</Quarter>
<SubmissionComment/>
<Version>1.2</Version>
<SummaryValueData>
<UnitID>001</UnitID>
<ParameterCode>CO2M</Param
<CurrentReportingPeriodTot
<OzoneSeasonToDateTotal/>
<YearToDateTotal>238270.80
</SummaryValueData>
<DailyTestSummaryData>
<UnitID>001</UnitID>
<Date>2009-01-01</Date>
<Hour>7</Hour>
<Minute>24</Minute>
<MonitoringSystemID/>
<ComponentID>013</Componen
<TestTypeCode>DAYCAL</Test
<TestResultCode>PASSED</Te
<SpanScaleCode/>
<DailyCalibrationData>
<OnLineOffLineIndicator>1<
<UpscaleGasCode>HIGH</Upsc
<ZeroInjectionDate>2009-01
<ZeroInjectionHour>7</Zero
<ZeroInjectionMinute>22</Z
<UpscaleInjectionDate>2009
<UpscaleInjectionHour>7</U
<UpscaleInjectionMinute>24
<ZeroMeasuredValue>0.000</
<UpscaleMeasuredValue>2.60
<ZeroAPSIndicator>0</ZeroA
<UpscaleAPSIndicator>0</Up
<ZeroCalibrationError>0.00
<UpscaleCalibrationError>2
<ZeroReferenceValue>0.000<
<UpscaleReferenceValue>2.5
</DailyCalibrationData>
</DailyTestSummaryData>
<DailyTestSummaryData>
<UnitID>001</UnitID>
<Date>2009-01-01</Date>
<Hour>7</Hour>
<Minute>25</Minute>
<MonitoringSystemID/>
<ComponentID>013</Componen
<TestTypeCode>INTCHK</Test
<TestResultCode>PASSED</Te
<SpanScaleCode/>
</DailyTestSummaryData>
<HourlyOperatingData>
<UnitID>001</UnitID>
<Date>2009-03-20</Date>
<Hour>15</Hour>
<OperatingTime>1.00</Opera
<HourLoad>90</HourLoad>
<LoadUnitsOfMeasureCode>MW
<LoadRange>9</LoadRange>
<CommonStackLoadRange/>
<FcFactor>1840.0</FcFactor
<FdFactor/>
<FwFactor/>
<FuelCode/>
<MonitorHourlyValueData>
<ParameterCode>CO2C</Param
<UnadjustedHourlyValue>12.
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID>104</M
<ComponentID>014</Componen
<PercentAvailable>86.8</Pe
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>FLOW</Param
<UnadjustedHourlyValue>151
<AdjustedHourlyValue>15420
<MODCCode>01</MODCCode>
<MonitoringSystemID>103</M
<ComponentID>013</Componen
<PercentAvailable>99.9</Pe
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>NOXC</Param
<UnadjustedHourlyValue>107
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID/>
<ComponentID>015</Componen
<PercentAvailable/>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>SO2C</Param
<UnadjustedHourlyValue>51.
<AdjustedHourlyValue>51.80
<MODCCode>01</MODCCode>
<MonitoringSystemID>102</M
<ComponentID>012</Componen
<PercentAvailable>86.8</Pe
<MoistureBasis/>
</MonitorHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>CO2</Parame
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>111.6
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>401</Fo
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>HI</Paramet
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>1064.
<MODCCode/>
<MonitoringSystemID>104</M
<FormulaIdentifier>801</Fo
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>NOXR</Param
<UnadjustedHourlyValue>0.1
<AdjustedHourlyValue>0.187
<MODCCode>01</MODCCode>
<MonitoringSystemID>105</M
<FormulaIdentifier>501</Fo
<PercentAvailable>78.4</Pe
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>SO2</Parame
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>132.6
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>201</Fo
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
</HourlyOperatingData>
<HourlyOperatingData>
<UnitID>001</UnitID>
<Date>2009-03-31</Date>
<Hour>23</Hour>
<OperatingTime>1.00</Opera
<HourLoad>89</HourLoad>
<LoadUnitsOfMeasureCode>MW
<LoadRange>9</LoadRange>
<CommonStackLoadRange/>
<FcFactor>1840.0</FcFactor
<FdFactor/>
<FwFactor/>
<FuelCode/>
<MonitorHourlyValueData>
<ParameterCode>NOXC</Param
<UnadjustedHourlyValue>108
<AdjustedHourlyValue/>
<MODCCode>01</MODCCode>
<MonitoringSystemID/>
<ComponentID>015</Componen
<PercentAvailable/>
<MoistureBasis/>
</MonitorHourlyValueData>
<MonitorHourlyValueData>
<ParameterCode>SO2C</Param
<UnadjustedHourlyValue>57.
<AdjustedHourlyValue>57.00
<MODCCode>01</MODCCode>
<MonitoringSystemID>102</M
<ComponentID>012</Componen
<PercentAvailable>86.8</Pe
<MoistureBasis/>
</MonitorHourlyValueData>
<DerivedHourlyValueData>
<ParameterCode>SO2</Parame
<UnadjustedHourlyValue/>
<AdjustedHourlyValue>144.2
<MODCCode/>
<MonitoringSystemID/>
<FormulaIdentifier>201</Fo
<PercentAvailable/>
<OperatingConditionCode/>
<SegmentNumber/>
<FuelCode/>
</DerivedHourlyValueData>
</HourlyOperatingData>
</Emissions>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am unable to resolve the underlined code that was an example in the suggested link. I am not able to get the XmlReaderSettings in the VB.Net 2003. I have added the MSXML2 reference. Any suggestions.
Dim myXmlSettings As New XmlReaderSettings
Using readMovieInfo As XmlReader = XmlReader.Create("test.xml
While readMovieInfo.Read()
ShowXmlNode(readMovieInfo)
While readMovieInfo.MoveToNextAttribute()
ShowXmlNode(readMovieInfo)
End While
End While
End Using
ASKER
To anyone.
I think problem with the XmlReaderSettings is the version of .Net Framework. Even though the folder structure shows multiple Framework folders, the System.dll is showing version 1.xxxx
I am not finding an immediate solution to correct this. Please advise.
Thanks.
I think problem with the XmlReaderSettings is the version of .Net Framework. Even though the folder structure shows multiple Framework folders, the System.dll is showing version 1.xxxx
I am not finding an immediate solution to correct this. Please advise.
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ruffone
Sorry, I cannot open the VB files as I do not have the newer version of Visual Studio (I am working that). I will check out the video. Thanks for your input.
Sorry, I cannot open the VB files as I do not have the newer version of Visual Studio (I am working that). I will check out the video. Thanks for your input.
ASKER
ruffone and aboredman
Just wanted to update you on this questions. I have the Express Edition of Visual Studio 2008. I am in the process of determining which solution is going to work for me. Thanks for your input.
Just wanted to update you on this questions. I have the Express Edition of Visual Studio 2008. I am in the process of determining which solution is going to work for me. Thanks for your input.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is posible to devide the points. Click the "Accept Multiple Solutions" button
ASKER