创建自定义XML映射

问题描述 投票:3回答:3

我正在尝试使用Excel VBA创建自定义XML映射。我的XML文件是BookData.xml

<?xml version='1.0'?>
<BookInfo>
   <Book>
      <ISBN>989-0-487-04641-2</ISBN>
      <Title>My World</Title>
      <Author>Nancy Davolio</Author>
      <Quantity>121</Quantity>
   </Book>
   <Book>
      <ISBN>981-0-776-05541-0</ISBN>
      <Title>Get Connected</Title>
      <Author>Janet Leverling</Author>
      <Quantity>435</Quantity>
   </Book>
   <Book>
      <ISBN>999-1-543-02345-2</ISBN>
      <Title>Honesty</Title>
      <Author>Robert Fuller</Author>
      <Quantity>315</Quantity>
   </Book>
</BookInfo>

我在Excel 2013中的VBA代码是:

Sub Create_XSD()
   Dim StrMyXml As String, MyMap As XmlMap
   Dim StrMySchema As String
   StrMyXml = "< BookInfo >"
   StrMyXml = StrMyXml & "<Book>"
   StrMyXml = StrMyXml & "<ISBN>Text</ISBN>"
   StrMyXml = StrMyXml & "<Title>Text</Title>"
   StrMyXml = StrMyXml & "<Author>Text</Author>"
   StrMyXml = StrMyXml & "<Quantity>999</Quantity>"
   StrMyXml = StrMyXml & "</Book>"
   StrMyXml = StrMyXml & "<Book></Book>"
   StrMyXml = StrMyXml & "</ BookInfo >"

   ' Turn off async loading.
   Application.DisplayAlerts = False
   ' Add the string to the XmlMaps collection.
   Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)
   Application.DisplayAlerts = True

   ' Create an empty file and output the schema.
   StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML
   Open "C:\Documents\MySchema.xsd" For Output As #1
   Print #1, StrMySchema
   Close #1
End Sub

这是返回错误:

Error

在线:

   Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)

我正在尝试理解如何使用VBA构建自定义XML映射的逻辑,以便我可以在另一个(更复杂的)XML文件上使用它。我关注的代码是here

那么,如何修复此错误?

xml excel vba excel-vba
3个回答
1
投票

删除"</ BookInfo >"中的空格


1
投票

您必须删除第2行上的逗号(在Dim StrMyXml As String之后)并将该行的其余部分放在它自己的行中,并在其前面使用命令Dim。 它应该如下所示:

Dim StrMyXml As String

Dim MyMap As XmlMap


0
投票

我遇到了同样的错误,BruceWayne的反应快速正确!

我删除了</ BookInfo>中的空格(第12行),但也在<BookInfo>中删除了空格(第4行)

仅在</ BookInfo>中删除是不够的。

© www.soinside.com 2019 - 2024. All rights reserved.