【发布时间】:2018-03-07 03:17:24
【问题描述】:
我在访问此 xml 文档中的最后一个节点时遇到问题,该文档遵循
的格式 <?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:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2016-11-29T14:40:38Z</Created>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>8748</WindowHeight>
<WindowWidth>19428</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>108</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s63">
<NumberFormat ss:Format="Fixed"/>
</Style>
<Style ss:ID="s64">
<NumberFormat ss:Format="0"/>
</Style>
</Styles>
<Worksheet ss:Name="xml File">
<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="771" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="14.55">
<Column ss:Width="57"/>
<Column ss:Width="56.4" ss:Span="1"/>
<Column ss:Index="4" ss:Width="50.4" ss:Span="3"/>
<Row ss:AutoFitHeight="0">
<Cell>
<Data ss:Type="String">Date</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
</Row>
我已尝试使用以下内容,但遇到了问题。读取 lastnode 元素只会返回几乎所有的 xml 文档
字符串 id = "2003-06-16T00:00:00.000"; //要选择的id
XElement Contact = (from xml2 in doc.Descendants("cell")
where xml2.Element("data").Value == id
select xml2).FirstOrDefault();
【问题讨论】:
-
单元格和“id”的数据类型是什么?他们可能不匹配。使用 Value 属性不是一个好主意。最好使用以下内容: (int)xml2.Element("data")
-
您还需要一个命名空间,因此请尝试以下操作:XElement root = doc.Root; XNamespace ns = root.GetDefaultNamespace(); XElement Contact = (from xml2 in doc.Descendants(ns + "cell") where (int)xml2.Element(ns + "data") == id select xml2).FirstOrDefault();