perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » focus » msxml » ask a question » past questions & answers Thursday, 11 October 2007
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services


Go back to list of previously asked questions and answers

Question: I am using MSXML to create an XML document; however, I am not able to find any property/method that would allow me to set the schemaLocation.
Asked By: Girish Subbanna
Viewed: 3665
Answer: MSXML (and DOM) does not provide any direct way of specifying the schemaLocation. The workaround is to add schemaLocation nodes like any other attribute nodes.

Visual Basic example that uses MSXML 4.0 DOM to create an XML document and specify schemaLocation:
Dim objNewXMLDoc As New MSXML2.DOMDocument40
Dim rootElement As IXMLDOMElement

objNewXMLDoc.appendChild _
     objNewXMLDoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")

Set objNewXMLDoc.documentElement = _
       objNewXMLDoc.createNode(NODE_ELEMENT, "x:doc", "http://xsdtesting")

Set rootElement = objNewXMLDoc.documentElement

rootElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
rootElement.setAttribute "xsi:schemaLocation", "http://xsdtesting 1.xsd"


objNewXMLDoc.save "c:\1.xml"

Visual Basic example that uses MXXMLWriter40 to create an XML document and specify the schemaLocation along with the root node:
Dim objXMLWriter As New MSXML2.MXXMLWriter40
Dim contHandler As MSXML2.IVBSAXContentHandler
Dim attrbs As New MSXML2.SAXAttributes40

Set contHandler = objXMLWriter

objXMLWriter.indent = True
objXMLWriter.omitXMLDeclaration = False
objXMLWriter.version = "1.0"
objXMLWriter.encoding = "utf-8"
objXMLWriter.standalone = False

contHandler.startDocument
attrbs.addAttribute "", "x", "xmlns:x", "", "http://xsdtesting"
attrbs.addAttribute "", "xsi", "xmlns:xsi", "", "http://www.w3.org/2001/XMLSchema-instance"
attrbs.addAttribute "", "schemaLocation", "xsi:schemaLocation", "", "http://xsdtesting 1.xsd"

contHandler.startElement "http://xsdtesting", "doc", "x:doc", attrbs
contHandler.endElement "http://xsdtesting", "doc", "x:doc"

contHandler.endDocument

MsgBox objXMLWriter.output

The above example create the following XML:
<?xml version="1.0"?>
<x:doc  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:x='http://xsdtesting'
    xsi:schemaLocation='http://xsdtesting 1.xsd'>
/>


Go back to list of previously asked questions and answers
  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy