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 creating an XML document using MXXMLWriter40 (a coClass that implements IMXWriter). However, I am not able to specify the namespace for the generated element. Can you please suggest how can I create a XML document that has elements with namespaces using MXXMLWriter40.
Asked By: Guest
Viewed: 3885
Answer: While creating the XML document, MXXMLWriter40 does not check if the document is well-formed, valid, or support namespaces. It simply writes the details based on the event methods called on it. In other words, there is no direct support for creating elements with namespaces. The workaround is to manually create the namespace declarations as attributes.

The following VB 6.0 code uses MSXML 4.0 and illustrates how to create an XML document using MXXMLWriter40 - the generated XML document contains elements with namespace declaration.
    Dim writer As New MXXMLWriter40
    Dim CH As IVBSAXContentHandler
    Dim attrib As New SAXAttributes40
        
    writer.encoding = "UTF-8"
    writer.indent = True
    Set CH = writer
        
    CH.startDocument
    
    attrib.addAttribute "", "myNS", "xmlns:myNS", "", "http://testuri"
    
    CH.startElement "http://testuri", "WebSites", "myNS:WebSites", attrib
       
    CH.characters "perfectxml.com"
        
    CH.endElement "http://testuri", "WebSites", "myNS:WebSites"
        
    CH.endDocument
    
    Debug.Print writer.output
    MsgBox writer.output
The above Visual Basic code generates the following XML:
<?xml version="1.0" standalone="no"?>
<myNS:WebSites xmlns:myNS="http://testuri">perfectxml.com</myNS:WebSites>

Related Links:


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