2016-04-08 48 views
1

一直在嘗試,並沒有得到任何工作結果。如何按位置選擇XML節點(Linq或XPATH)

鑑於是Excel這樣的XML結構:

<?xml version="1.0"?> 
<?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"> 
... 
</DocumentProperties> 
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> 
... 
</OfficeDocumentSettings> 
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
... 
</ExcelWorkbook> 
<Styles> 
... 
</Styles> 
<Worksheet ss:Name="Report"> 
    <Table ss:ExpandedColumnCount="41" ss:ExpandedRowCount="4082" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultColumnWidth="60" ss:DefaultRowHeight="15"> 
    <Row> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_1</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_2</Data></Cell> 
    ... 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_40_Active</Data></Cell> 
    </Row> 
    <Row> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_1</Data></Cell> 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_2</Data></Cell> 
    ... 
    <Cell ss:StyleID="s62"><Data ss:Type="String">Cell_40_Active</Data></Cell> 
    </Row> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
... 
    </WorksheetOptions> 
</Worksheet> 
</Workbook> 

目標是隻選擇這些行,其中包含的行的「Cell_40_Active」 40-小區(ID)之內。像:細胞[40] .Data.InnerText = 「Cell_40_Active」 ......

 XmlDocument doc = new XmlDocument(); 
     doc.Load(file); 
     XmlElement root = doc.DocumentElement; 
     // does return all Row-elements >> working 
     XmlNodeList nodes = root.GetElementsByTagName("Row"); 
     //does not return any element (0) 
     XmlNodeList nodes = root.SelectNodes("/Worksheet/Row/Cell[40]='Cell_40_Active'"); 

如何才能做到這一點?還沒有找到類似的... 任何提示?非常感謝你。

回答

1

所有的元素都是在命名空間urn:schemas-microsoft-com:office:spreadsheet所以你必須爲準備:

var nsmgr = new XmlNamespaceManager(doc.NameTable); 
nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:office:spreadsheet"); 
XmlNodeList nodes = root.SelectNodes("<xpath expr using x prefix>", nsmgr); 

並給予你的描述,則XPath表達式也許應該是(使用前面定義的前綴x):

/x:Workbook/x:Worksheet/x:Table/x:Row[x:Cell[40]/x:Data='Cell_40_Active'] 
+0

這個伎倆......謝謝你,老兄! – SiL3NC3

0

也許你可以利用XmlNodeList的索引函數,比如說在Linq查詢的上下文中,比如:

var result = from row in root.GetElementsByTagName("Row") 
      where row.index(40).InnerText == "Cell_40_Active" 
      //uses inner text to "skip over" the data tag. 
      //this won't work if you have other child nodes with inner text. 
      select row; 
+0

不能與XmlNodeList中(對於那些方法沒有定義)... 錯誤使用LINQ:嚴重性\t代碼\t說明\t項目\t文件\t線\t抑制狀態 錯誤\t CS1934 \t找不到源類型的查詢模式的實現'XmlNodeList中'。 '哪裏找不到'。考慮顯式指定範圍變量'row'的類型。 – SiL3NC3

+0

我想你會需要切換到一些更手動的,有一個列表,一個先進的循環,和一個if語句。我沒有意識到XmlNodeList沒有爲LINQ設置。 – sowrd299