在简单的Excel工作表中编辑元素值时无法找到XML节点

时间:2015-04-03 22:14:40

标签: xml vb.net

我正在处理一个简单的XML文件来生成Excel文件。 我遇到的问题是编辑工作表名称。每次我尝试更新值时,都会出现NullReferenceException。

这是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">
  <Author>Me</Author>
  <Company>RMyCo, LLC</Company>
  <Version>1.0</Version>
  </DocumentProperties>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>6795</WindowHeight>
    <WindowWidth>8460</WindowWidth>
    <WindowTopX>120</WindowTopX>
    <WindowTopY>15</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
      <Borders />
      <Font />
      <Interior />
      <NumberFormat />
      <Protection />
    </Style>
    <Style ss:ID="s21">
      <Font x:Family="Swiss" ss:Bold="1" />
    </Style>
  </Styles>
  <Worksheet ss:Name="Sheet1">
    <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="5"
       x:FullColumns="1" x:FullRows="1">
     </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <Print>
        <ValidPrinterInfo />
        <HorizontalResolution>600</HorizontalResolution>
        <VerticalResolution>600</VerticalResolution>
      </Print>
      <Selected />
      <Panes>
        <Pane>
          <Number>3</Number>
          <ActiveRow>5</ActiveRow>
          <ActiveCol>1</ActiveCol>
        </Pane>
      </Panes>
      <Table>
      </Table>
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </Worksheet>
</Workbook>

以下是代码:

Imports System
Imports System.Reflection
Imports System.Xml
Imports System.Xml.Linq

Public Class ExcelXML


Public Function RenameSheet(FileNamePath As String, OldSheetName As String, NewSheetName As String) As Integer

        Dim xDoc As New XmlDocument
        Dim xRead As New XmlTextReader(FileNamePath)
        Dim xNode As XmlNode

        xDoc.Load(xRead)

        xNode = xDoc.SelectSingleNode("/Workbook/Worksheet[@Name='Sheet1']")

        xNode.Attributes("Name").Value = NewSheetName

        xDoc.Save(FileNamePath)
        xRead.Close()


        Return 0

    End Function

End Class

每当我尝试重命名工作表时,它就会消失。

xNode.Attributes("Name").Value = NewSheetName

1 个答案:

答案 0 :(得分:0)

您的SelectSingleNode()已退回Nothing,并使用Nothing进行操作可保证NullReferenceException(请阅读相关问题以获取有关此主题的更多说明)。

您的XML有各种名称空间,需要在XPath中考虑它们。在这种情况下,两个相关的命名空间是默认命名空间(没有前缀的命名空间,如xmlns="...")和ss命名空间前缀。

要使用XmlDoocument在命名空间中获取元素/属性,您需要将prefix-to-namespace_uri映射注册到XmlNamespaceManager,然后在XPath中正确使用已注册的前缀:

......

'initialize namespace manager'
Dim nsManager As New XmlNamespaceManager(New NameTable())

'register namespace prefixes'
nsManager.AddNamespace("d", "urn:schemas-microsoft-com:office:spreadsheet")
nsManager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet")

'modify the XPath to utilize prefixes, and pass namespace manager as 2nd args'
Dim xNode = xDoc.SelectSingleNode("/d:Workbook/d:Worksheet[@ss:Name='Sheet1']", nsManager)
xNode.Attributes("ss:Name").Value = NewSheetName

.......