8/20/2017 Creating an XML Mapping Schema in Excel 2010
Creating an XML Mapping Schema in Excel 2010
Office 2010
Getting Started with Excel 2010 Extensibility: Learn how to create and utilize Xml Mapping schemas in Excel 2010.
Last modified: May 09, 2011
Applies to: Excel 2010 | Office 2010 | VBA
In this article use an xlsb spreasheet,
Create the XML Data File follow everything, don't skip
Create the XML Mapping Schema File
Import the Schema and XML Data into Excel any step !!
Next Steps
Published: November 2010
In this exercise, you create a sample XML data file. You then create an XML Mapping schema by using one of two methods. You then use that schema to create a table mapped to the sam
task, you must do the following:
Create the XML Data File
Create the XML Mapping Schema File
Import the Schema and XML Data into Excel
Create the XML Data File
In this task, you create an XML data file in a text editor that is used later in this exercise.
To create the XML data file
1. Start the text editor such as Notepad.
2. Paste or type the following XML into the editor.
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>
3. Save the document as C:\[Link].
4. Close the text editor. change to D:\[Link]
Create the XML Mapping Schema File
In this task, you create a custom XML Mapping schema by using Microsoft Visual Basic for Applications (VBA). You can create a schema with either of two methods: by explicitly writing
by reading the XML file into a string and then creating the schema from that string.
[Link] 1/3
8/20/2017 Creating an XML Mapping Schema in Excel 2010
by reading the XML file into a string and then creating the schema from that string.
To create the XSD in Excel by explicitly writing the XML in the code
1. Create a blank workbook in Excel 2010.
2. Open the Visual Basic Editor by pressing Alt+F11.
3. Insert a general module by clicking Insert and then clicking Module.
4. In the module, paste or type the following code.
VBA
Sub Create_XSD()
Dim StrMyXml As String, MyMap As XmlMap
Dim StrMySchema As String
StrMyXml = "< BookInfo >" this code has error
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.
[Link] = False
' Add the string to the XmlMaps collection.
Set MyMap = [Link](StrMyXml)
[Link] = True
' Create an empty file and output the schema.
StrMySchema = [Link](1).Schemas(1).XML
Open "C:\[Link]" For Output As #1
Print #1, StrMySchema
Close #1
End Sub
5. Close the Visual Basic Editor.
6. Next, run the code. On the Developer tab, click Macros, highlight Create_XSD, and then click Run. Examine the schema file at C:\[Link]. Shortly, you will load this file int
To create the schema in Excel by reading the XML data into a string
1. In a blank Excel 2010 workbook, open the Visual Basic Editor by pressing Alt+F11.
2. Insert a general module by clicking Insert and then clicking Module.
3. In the module, paste or type the following code.
VBA
Sub Create_XSD2()
Dim StrMyXml As String, MyMap As XmlMap change to D:
Dim StrMySchema As String
' [Link] is the file created in section one of this topic.
StrMyXml = "C:\[Link]"
' Turn off async loading.
[Link] = False
' Add the string to the XmlMaps collection.
Set MyMap = [Link](StrMyXml)
[Link] = True
' Create an empty file and output the schema.
StrMySchema = [Link](1).Schemas(1).XML
Open "C:\[Link]" For Output As #1
Print #1, StrMySchema
Close #1
End Sub
4. Close the Visual Basic Editor.
[Link] 2/3
8/20/2017 Creating an XML Mapping Schema in Excel 2010 should be Create_XSD2
5. Next, run the code. On the Developers tab, click Macros, highlight Create_XSD, and then click Run. Examine the schema file at C:\[Link]. Shortly, you will load this file in
map.
if no developer, file ->
no need in options -> customise ribbon
new version -> click developer -> ok
Import the Schema and XML Data into Excel
no need for
In this task, you import the schema created in the previous section into Excel to create an XML map and then import an XML data file.
steps 2/3 if
To create the Xml Map and then import the XML data file into Excel bookinfo is
1. On the Developer tab, in the XML group, click Source to open the XML Source task pane.
already
there
2. On the task pane, click Xml Maps.
3. In the Xml Maps dialog box, click Add, navigate to the file [Link], click Open, and then click OK.
4. In the XML Source task pane, drag the BookInfo node to cell A1. This action creates a table in the worksheet as shown in Figure 1. You can format the table as necessary.
Figure 1. Table created from the Xml Map
5. Next, import the XML data file. On the Developer tab, click Import.
6. In the Import XML dialog box, navigate to the XML data file and then click Import. The data is mapped into the formatted table as shown in Figure 2.
Figure 2. XML data is imported into the table
Next Steps
Quick Note Scenario: Getting Started with Excel 2010 Extensibility
[Link]
[Link]
© 2017 Microsoft
[Link] 3/3