Reading Excel XML with C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I want to read Excel file that is saved as XML.
With following code I can read contents of the <Data> elements:

<pre>XmlDocument xml = new XmlDocument();
xml.Load(fi.FullName);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement root = xml.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//ss:Data", nsmgr);

IEnumerator ienum = nodeList.GetEnumerator();
while (ienum.MoveNext())
{
XmlNode title = (XmlNode)ienum.Current;
Console.WriteLine(title.InnerText);
} [/code]


The problem is that I dont want to read them all. The needed elements can be recognized from the attribute values.
How can I get the values of the Data elements where the Cell elements StyleIDs are i.e.:
s21, s23, s29, m29104078 and m29103774?
<pre><?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office </DocumentProperties>
<CustomDocumentProperties xmlns="urn:schemas-microsoft-com:office:office
<sflag dt:dt="string 1251339764</sflag>
</CustomDocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel
<WindowHeight>9120</WindowHeight>
<WindowWidth>14955</WindowWidth>
<WindowTopX>1920</WindowTopX>
<WindowTopY>240</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles></Styles>
<Worksheet ss:Name="Sheet1
<Table ss:ExpandedColumnCount="15" ss:ExpandedRowCount="76" x:FullColumns="1" x:FullRows="1" ss:StyleID="s22" ss:DefaultColumnWidth="60" ss:DefaultRowHeight="15
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="92.25"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="27.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="33.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="75.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="71.25"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="36"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="36.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="35.25"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="33.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="42"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="47.25"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="23.25"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="48.75"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="111"/>
<Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="98.25"/>
<Row ss:AutoFitHeight="0
<Cell ss:MergeAcross="12" ss:MergeDown="1" ss:StyleID="s182
<Data ss:Type="String Delivery Note</Data>
</Cell>
<Cell ss:StyleID="s21
<Data ss:Type="String DN No:</Data>
</Cell>
<Cell ss:StyleID="s21
<Data ss:Type="String ABC123</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0
<Cell ss:Index="14" ss:StyleID="s23
<Data ss:Type="String MR No:</Data>
</Cell>
<Cell ss:StyleID="s23
<Data ss:Type="String CBA321</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0
<Cell ss:MergeAcross="1" ss:StyleID="m29104534
<Data ss:Type="String Purpose of Delivery:</Data>
</Cell>
<Cell ss:MergeAcross="4" ss:StyleID="m29104544
<Data ss:Type="String To site(pick up by subcon)</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104554
<Data ss:Type="String Receiver:</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104564
<Data ss:Type="String Customer</Data>
</Cell>
<Cell ss:StyleID="s24
<Data ss:Type="String Print Date:</Data>
</Cell>
<Cell ss:StyleID="s28
<Data ss:Type="String 2010-10-28 21:39:21</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="24
<Cell ss:MergeAcross="1" ss:StyleID="m29104372
<Data ss:Type="String Delivery Address:</Data>
</Cell>
<Cell ss:MergeAcross="10" ss:StyleID="m29104382
<Data ss:Type="String NA</Data>
</Cell>
<Cell ss:StyleID="s29
<Data ss:Type="String Receiver Tel:</Data>
</Cell>
<Cell ss:StyleID="m29104118
<Data ss:Type="String 5555555555</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="20.25
<Cell ss:MergeAcross="1" ss:StyleID="m29104392
<Data ss:Type="String Site:</Data>
</Cell>
<Cell ss:MergeAcross="4" ss:StyleID="s164
<Data ss:Type="String OUTOK</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104412
<Data ss:Type="String Request Arrived Date:</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104422
<Data ss:Type="String 2010-11-30 14:11:31</Data>
</Cell>
<Cell ss:StyleID="s30
<Data ss:Type="String Request Shipment Date:</Data>
</Cell>
<Cell ss:StyleID="s31
<Data ss:Type="String </Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0
<Cell ss:MergeAcross="1" ss:StyleID="m29104220
<Data ss:Type="String Contract Info:</Data>
</Cell>
<Cell ss:MergeAcross="4" ss:StyleID="s176
<Data ss:Type="String 123456789D</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104240
<Data ss:Type="String Logistics Specialist:</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104250
<Data ss:Type="String </Data>
</Cell>
<Cell ss:StyleID="s24
<Data ss:Type="String Tel:</Data>
</Cell>
<Cell ss:StyleID="s28
<Data ss:Type="String </Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0
<Cell ss:MergeAcross="1" ss:StyleID="m29104068
<Data ss:Type="String Project Name:</Data>
</Cell>
<Cell ss:MergeAcross="10" ss:StyleID="m29104078
<Data ss:Type="String Project 123</Data>
</Cell>
<Cell ss:StyleID="s24
<Data ss:Type="String Project Code:</Data>
</Cell>
<Cell ss:StyleID="s28
<Data ss:Type="String 556677</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="21.75
<Cell ss:MergeAcross="1" ss:StyleID="m29104088
<Data ss:Type="String Product Category:</Data>
</Cell>
<Cell ss:MergeAcross="4" ss:StyleID="m29104098
<Data ss:Type="String </Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104108
<Data ss:Type="String Customer:</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29104118
<Data ss:Type="String DataCorp</Data>
</Cell>
<Cell ss:StyleID="s24
<Data ss:Type="String Customer PO:</Data>
</Cell>
<Cell ss:StyleID="s32
<Data ss:Type="String 2098</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="24
<Cell ss:MergeAcross="1" ss:StyleID="m29103916
<Data ss:Type="String Special Unloading Req.:</Data>
</Cell>
<Cell ss:MergeAcross="4" ss:StyleID="m29103926
<Data ss:Type="String N/A</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29103936
<Data ss:Type="String Product Manager:</Data>
</Cell>
<Cell ss:MergeAcross="2" ss:StyleID="m29103946
<Data ss:Type="String </Data>
</Cell>
<Cell ss:StyleID="s24
<Data ss:Type="String Tel:</Data>
</Cell>
<Cell ss:StyleID="s28
<Data ss:Type="String </Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="24
<Cell ss:MergeAcross="1" ss:StyleID="m29103764
<Data ss:Type="String Installation Environment:</Data>
</Cell>
<Cell ss:MergeAcross="12" ss:StyleID="m29103774
<Data ss:Type="String Outdoors</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0
<Cell ss:MergeAcross="1" ss:StyleID="m29103784
<Data ss:Type="String Description(MR):</Data>
</Cell>
<Cell ss:MergeAcross="12" ss:StyleID="m29103794
<Data ss:Type="String </Data>
</Cell>
</Row><br/>.<br/>.<br/>.<br/><br/>[/code]
<br/>
Br,
Tupe
<br/>
<br/>

View the full article
 
Back
Top