EDN Admin
Well-known member
Hi so im working on a project in c#. I have hundreds of xml files and I need to extract 6 values from each of the xml files and somehow display them on an excel datasheet. Here is an example of the xml file:<br/>
<br/>
<XAxisCalib><br/>
<Max>288.93</Max><br/>
<br/>
<Min>-48.08</Min><br/>
<br/>
<MaxS>200</MaxS><br/>
<br/>
<DfTHt>0</DfTHt><br/>
<br/>
<LdVe>0</LdVe><br/>
<br/>
<EjVe>0</EjVe><br/>
<br/>
</XAxisCalib><br/>
<br/>
<YAxisCalib><br/>
<br/>
<Max>646.01</Max><br/>
<br/>
<Min>-90.9</Min><br/>
<br/>
<MaxS>200</MaxS><br/>
<br/>
<DfTHt>0</DfTHt><br/>
<br/>
<LdVe>0</LdVe><br/>
<br/>
<EjVe>0</EjVe><br/>
<br/>
</YAxisCalib><br/>
<br/>
<ZAxisCalib><br/>
<br/>
<Max>16.1</Max><br/>
<br/>
<Min>-146.82</Min><br/>
<br/>
I need to extract the <Max> and <Min> value of the x,y,and z calib. How can I do this. Ive done tons of research but no examples show some of the xml file all sources are to extract all of the xml values to the excel sheet. This is an example of
what I have so far tell me if Im on the right track....<br/>
<br/>
using System.Collections.Generic;<br/>
using System.Configuration;<br/>
using System.Text;<br/>
using System.Text.RegularExpressions;<br/>
using System.IO;<br/>
using System.Xml.XPath;<br/>
using System.Xml;<br/>
using System.Collections;<br/>
using System.Diagnostics;<br/>
using Microsoft.Office.Interop.Excel; <br/>
<br/>
namespace MaxMinParser<br/>
{<br/>
class Program<br/>
{<br/>
static void Main(string[] args)<br/>
{<br/>
using System;<br/>
<br/>
//calls for Sciclone Cfg files to be accessed<br/>
string appContent = ""; <br/>
FileStream appFile = new FileStream("c:\Sciclone UAC.cfg", FileMode.Open, FileAccess.ReadWrite);<br/>
StreamReader myReader = new StreamReader(appFile);<br/>
appContent = myReader.ReadToEnd();<br/>
appFile.Close();<br/>
myReader.Close();<br/>
<br/>
SummaryRecord summaryRecord;<br/>
SummaryRecordsCollection summaryRecords = new SummaryRecordsCollection();<br/>
<br/>
//creates a summary/results file of the MaxMin Values<br/>
<br/>
ResultsParser resultsParser;<br/>
string summaryFile = Properties.Settings.Default.SummaryFile;<br/>
int resultCount = Int32.Parse(Properties.Settings.Default.ResultCount); <br/>
<br/>
resultsParser = new ResultsParser();<br/>
<br/>
<br/>
Stream xmlStream = GetXMLStream(fileName);<br/>
System.Diagnostics.Debug.WriteLine(fileName);<br/>
XmlReaderSettings settings = new XmlReaderSettings();<br/>
settings.ConformanceLevel = ConformanceLevel.Document;<br/>
settings.IgnoreComments = true;<br/>
settings.IgnoreWhitespace = true;<br/>
XmlReader reader = XmlReader.Create(xmlReader, Settings);<br/>
<br/>
//Using the config files, calls for the X,Y,Z max/min values for each build<br/>
XPathDocument resultsXMLDoc = new XPathDocument(reader);<br/>
int XmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<XAxisCalib>" + result "</Max>");<br/>
int XminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<XAxisCalib>" + result "</Min>");<br/>
int YmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<YAxisCalib>" + result"</Max>");<br/>
int YminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<YAxisCalib>" + result "</Min>");<br/>
int ZmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<ZAxisCalib>" + result "</Max>");<br/>
int ZminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<ZAxisCalib>" + result "</Min>");<br/>
summaryRecord = new SummaryRecord(fileName, XmaxTests, XminTests, YmaxTests, YminTests, ZmaxTests, ZminTests);<br/>
summaryRecords.AddRecords(summaryRecord);<br/>
}<br/>
}<br/>
}<br/>
}<br/>
if (summaryRecords.RecordCount > 0)<br/>
{<br/>
if (File.Exists(summaryFile))<br/>
{<br/>
File.Delete(summaryFile);<br/>
}<br/>
StreamWriter MaxMinResults = new StreamWriter(summaryFile, false);<br/>
<br/>
<br/>
foreach(SummaryRecord record in summaryRecords)<br/>
{<br/>
MaxMinResults.WriteLine("<SummaryRecord>");<br/>
MaxMinResults.WriteLine("<XMax>" + record.XMax + "</XMax>");<br/>
MaxMinResults.WriteLine("<XMin>" + record.XMin + "</XMin>");<br/>
MaxMinResults.WriteLine("<YMax>" + record.YMax + "</YMax>");<br/>
MaxMinResults.WriteLine("<YMin>" + record.YMin + "</YMin>");<br/>
MaxMinResults.WriteLine("<ZMax>" + record.ZMax + "</ZMax>");<br/>
MaxMinResults.WriteLine("<ZMin>" + record.ZMin + "</ZMin>");<br/>
MaxMinResults.WriteLine("</SummaryRecord>");<br/>
MaxMinResults.Flush();<br/>
<br/>
{<br/>
Excel.Workbooks objBooks;<br/>
Excel.Sheets objSheets;<br/>
Excel._Worksheet objSheet;<br/>
Excel.Range range;<br/>
try<br/>
{<br/>
//opens Excel workbook and creates a worksheet and calls for sheet1<br/>
{ <br/>
m_objExcel = new Excel.Application();<br/>
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;<br/>
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));<br/>
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;<br/>
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));<br/>
<br/>
<br/>
// Creates an array for the headers and add it to cells A1 through F1.<br/>
object[] objHeaders = {"XMax", "XMin", "YMax", "YMin", "ZMax", "ZMin"};<br/>
m_objRange = m_objSheet.get_Range("A1", "F1");<br/>
m_objRange.Value = objHeaders;<br/>
<br/>
//creates an array with 200 rows and 6 columns that will<br/>
// add the specified data to the specified row and column<br/>
object[,] objData = new Object[200,6];<br/>
for(int r=0;r<200;r++)<br/>
{<br/>
objData[r,0] = ("int XMaxTests" + value);<br/>
<br/>
objData[r,1] = ("int XMinTests" + value);<br/>
<br/>
objData[r,2] = ("int YMaxTests" + value);<br/>
<br/>
objData[r,3] = ("int YMinTests" + value);<br/>
<br/>
objData[r,4] = ("int ZMaxTests" + value);<br/>
<br/>
objData[r,5] = ("int ZMinTests" + value);<br/>
<br/>
}<br/>
}<br/>
m_objRange = m_objSheet.get_Range("A2", m_objOpt);<br/>
m_objRange = m_objRange.get_Resize(200,6);<br/>
m_objRange.Value = objData;<br/>
<br/>
<br/>
// Save the Workbook<br/>
m_objBook.SaveAs(m_strSampleFolder + "MaxMinValues.xls", m_objOpt, m_objOpt, <br/>
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, <br/>
m_objOpt, m_objOpt, m_objOpt, m_objOpt);<br/>
<br/>
m_objBook.Close(false, m_objOpt, m_objOpt);<br/>
m_objExcel.Quit();<br/>
}<br/>
}<br/>
}<br/>
<br/>
<br/>
Any help would be appreciated please...?
View the full article
<br/>
<XAxisCalib><br/>
<Max>288.93</Max><br/>
<br/>
<Min>-48.08</Min><br/>
<br/>
<MaxS>200</MaxS><br/>
<br/>
<DfTHt>0</DfTHt><br/>
<br/>
<LdVe>0</LdVe><br/>
<br/>
<EjVe>0</EjVe><br/>
<br/>
</XAxisCalib><br/>
<br/>
<YAxisCalib><br/>
<br/>
<Max>646.01</Max><br/>
<br/>
<Min>-90.9</Min><br/>
<br/>
<MaxS>200</MaxS><br/>
<br/>
<DfTHt>0</DfTHt><br/>
<br/>
<LdVe>0</LdVe><br/>
<br/>
<EjVe>0</EjVe><br/>
<br/>
</YAxisCalib><br/>
<br/>
<ZAxisCalib><br/>
<br/>
<Max>16.1</Max><br/>
<br/>
<Min>-146.82</Min><br/>
<br/>
I need to extract the <Max> and <Min> value of the x,y,and z calib. How can I do this. Ive done tons of research but no examples show some of the xml file all sources are to extract all of the xml values to the excel sheet. This is an example of
what I have so far tell me if Im on the right track....<br/>
<br/>
using System.Collections.Generic;<br/>
using System.Configuration;<br/>
using System.Text;<br/>
using System.Text.RegularExpressions;<br/>
using System.IO;<br/>
using System.Xml.XPath;<br/>
using System.Xml;<br/>
using System.Collections;<br/>
using System.Diagnostics;<br/>
using Microsoft.Office.Interop.Excel; <br/>
<br/>
namespace MaxMinParser<br/>
{<br/>
class Program<br/>
{<br/>
static void Main(string[] args)<br/>
{<br/>
using System;<br/>
<br/>
//calls for Sciclone Cfg files to be accessed<br/>
string appContent = ""; <br/>
FileStream appFile = new FileStream("c:\Sciclone UAC.cfg", FileMode.Open, FileAccess.ReadWrite);<br/>
StreamReader myReader = new StreamReader(appFile);<br/>
appContent = myReader.ReadToEnd();<br/>
appFile.Close();<br/>
myReader.Close();<br/>
<br/>
SummaryRecord summaryRecord;<br/>
SummaryRecordsCollection summaryRecords = new SummaryRecordsCollection();<br/>
<br/>
//creates a summary/results file of the MaxMin Values<br/>
<br/>
ResultsParser resultsParser;<br/>
string summaryFile = Properties.Settings.Default.SummaryFile;<br/>
int resultCount = Int32.Parse(Properties.Settings.Default.ResultCount); <br/>
<br/>
resultsParser = new ResultsParser();<br/>
<br/>
<br/>
Stream xmlStream = GetXMLStream(fileName);<br/>
System.Diagnostics.Debug.WriteLine(fileName);<br/>
XmlReaderSettings settings = new XmlReaderSettings();<br/>
settings.ConformanceLevel = ConformanceLevel.Document;<br/>
settings.IgnoreComments = true;<br/>
settings.IgnoreWhitespace = true;<br/>
XmlReader reader = XmlReader.Create(xmlReader, Settings);<br/>
<br/>
//Using the config files, calls for the X,Y,Z max/min values for each build<br/>
XPathDocument resultsXMLDoc = new XPathDocument(reader);<br/>
int XmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<XAxisCalib>" + result "</Max>");<br/>
int XminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<XAxisCalib>" + result "</Min>");<br/>
int YmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<YAxisCalib>" + result"</Max>");<br/>
int YminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<YAxisCalib>" + result "</Min>");<br/>
int ZmaxTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<ZAxisCalib>" + result "</Max>");<br/>
int ZminTests = resultsParser.GetTestResultCount(resultsXMLDoc, "<ZAxisCalib>" + result "</Min>");<br/>
summaryRecord = new SummaryRecord(fileName, XmaxTests, XminTests, YmaxTests, YminTests, ZmaxTests, ZminTests);<br/>
summaryRecords.AddRecords(summaryRecord);<br/>
}<br/>
}<br/>
}<br/>
}<br/>
if (summaryRecords.RecordCount > 0)<br/>
{<br/>
if (File.Exists(summaryFile))<br/>
{<br/>
File.Delete(summaryFile);<br/>
}<br/>
StreamWriter MaxMinResults = new StreamWriter(summaryFile, false);<br/>
<br/>
<br/>
foreach(SummaryRecord record in summaryRecords)<br/>
{<br/>
MaxMinResults.WriteLine("<SummaryRecord>");<br/>
MaxMinResults.WriteLine("<XMax>" + record.XMax + "</XMax>");<br/>
MaxMinResults.WriteLine("<XMin>" + record.XMin + "</XMin>");<br/>
MaxMinResults.WriteLine("<YMax>" + record.YMax + "</YMax>");<br/>
MaxMinResults.WriteLine("<YMin>" + record.YMin + "</YMin>");<br/>
MaxMinResults.WriteLine("<ZMax>" + record.ZMax + "</ZMax>");<br/>
MaxMinResults.WriteLine("<ZMin>" + record.ZMin + "</ZMin>");<br/>
MaxMinResults.WriteLine("</SummaryRecord>");<br/>
MaxMinResults.Flush();<br/>
<br/>
{<br/>
Excel.Workbooks objBooks;<br/>
Excel.Sheets objSheets;<br/>
Excel._Worksheet objSheet;<br/>
Excel.Range range;<br/>
try<br/>
{<br/>
//opens Excel workbook and creates a worksheet and calls for sheet1<br/>
{ <br/>
m_objExcel = new Excel.Application();<br/>
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;<br/>
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));<br/>
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;<br/>
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));<br/>
<br/>
<br/>
// Creates an array for the headers and add it to cells A1 through F1.<br/>
object[] objHeaders = {"XMax", "XMin", "YMax", "YMin", "ZMax", "ZMin"};<br/>
m_objRange = m_objSheet.get_Range("A1", "F1");<br/>
m_objRange.Value = objHeaders;<br/>
<br/>
//creates an array with 200 rows and 6 columns that will<br/>
// add the specified data to the specified row and column<br/>
object[,] objData = new Object[200,6];<br/>
for(int r=0;r<200;r++)<br/>
{<br/>
objData[r,0] = ("int XMaxTests" + value);<br/>
<br/>
objData[r,1] = ("int XMinTests" + value);<br/>
<br/>
objData[r,2] = ("int YMaxTests" + value);<br/>
<br/>
objData[r,3] = ("int YMinTests" + value);<br/>
<br/>
objData[r,4] = ("int ZMaxTests" + value);<br/>
<br/>
objData[r,5] = ("int ZMinTests" + value);<br/>
<br/>
}<br/>
}<br/>
m_objRange = m_objSheet.get_Range("A2", m_objOpt);<br/>
m_objRange = m_objRange.get_Resize(200,6);<br/>
m_objRange.Value = objData;<br/>
<br/>
<br/>
// Save the Workbook<br/>
m_objBook.SaveAs(m_strSampleFolder + "MaxMinValues.xls", m_objOpt, m_objOpt, <br/>
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, <br/>
m_objOpt, m_objOpt, m_objOpt, m_objOpt);<br/>
<br/>
m_objBook.Close(false, m_objOpt, m_objOpt);<br/>
m_objExcel.Quit();<br/>
}<br/>
}<br/>
}<br/>
<br/>
<br/>
Any help would be appreciated please...?
View the full article