1

我对命名空间的工作方式感到困惑。我正在尝试获取worksheetwheress:Name="Datagrid"并从中获取data节点 wherename="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>
4

2 回答 2

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 文档中称为轴属性。

我已将您的 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)
于 2012-08-17T13:15:19.120 回答
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""]")
于 2012-08-16T18:02:54.170 回答