如何使用命名空间使用...语法创建linq查询

时间:2012-08-16 16:11:17

标签: xml vb.net linq

我对命名空间如何工作感到困惑。我正在尝试将worksheet放在ss:Name="Datagrid"的位置,然后从data节点获取name="emailname"

Imports <xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
Module Module1   
    Sub Main()
        Dim xmlFile As String = System.AppDomain.CurrentDomain.BaseDirectory & "Datagrid.xml"
        Dim root As XElement = XElement.Load(xmlFile)

    ''select worksheet where ss:Name="Datagrid""
    'Dim dg = From item In root .......................

    ''get data from wokrsheet...table..row...data where = name="emailname"  (not ss:name="emailname")

    'Dim data = From item In dg .......................
    End Sub
End Module

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">
  <Worksheet ss:Name="Datagrid">
    <Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1"
     x:FullRows="1" ss:DefaultRowHeight="15">
      <Row ss:Index="3" ss:AutoFitHeight="0">
        <Cell Name="emailname">
          <Data ss:Type="String">email address</Data>
        </Cell>
      </Row>
      <Row ss:Index="4" ss:AutoFitHeight="0">
        <Cell Name="username">
          <Data ss:Type="String">user name</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
  <Worksheet ss:Name="Properties">
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="3" x:FullColumns="1"
     x:FullRows="1" ss:DefaultRowHeight="15">
      <Row>
        <Cell>
          <Data ss:Type="Number">1</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="Number">2</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="Number">3</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

2 个答案:

答案 0 :(得分:1)

使用文件顶部的Imports语句声明名称空间。然后,您可以使用<ns:name>语法来引用它们。

所以

Imports <xmlns="urn:schemas-microsoft-com:office:spreadsheet">
Imports <xmlns:o="urn:schemas-microsoft-com:office:office">
Imports <xmlns:x="urn:schemas-microsoft-com:office:excel">
Imports <xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
Imports <xmlns:html="http://www.w3.org/TR/REC-html40">

将为您提供由您提供的XML定义的命名空间。

现在您可以访问<Workbook>...<Table>.@x:FullRows,包括在LINQ查询中。

VB.NET的这些XML功能在Microsoft文档中称为Axis Properties

我已将您的XML粘贴到VS2008中,并确定您的问题是XML标记(包括Axis属性)是敏感的

''select worksheet where ss:Name="Datagrid""
'Dim dg = From item In root .......................
Dim dg = root.<Worksheet>.FirstOrDefault(Function(w) w.@ss:Name = "Datagrid")

If dg Is Nothing Then _
    Throw New Exception("DataGrid not found")

''get data from wokrsheet...table..row...data where = name="emailname"  (not ss:name="emailname")

'Dim data = From item In dg .......................
' Both of the following lines work, but the second answers the question in the title:
'Dim data = dg.<Table>.<Row>.<Cell>.FirstOrDefault(Function(d) d.@Name = "emailname")
Dim data = dg...<Cell>.FirstOrDefault(Function(d) d.@Name = "emailname")

If data Is Nothing Then _
    Throw New Exception("emailname not found")

Console.WriteLine(data.<Data>.Value)

答案 1 :(得分:0)

要获得更清晰的语法,建议尝试使用下面显示的Linq-to-XML XPath扩展方法。

更新#1:如果XML文件不包含任何名称空间,则以下代码有效。研究考虑命名空间的解决方案。

Dim root = XElement.Load(xmlFile)
Dim data = root.XPathSelectElements("Worksheet[@Name=""Datagrid""]/Table/Row/Cell/Data[@Name=""edata""]")
相关问题