Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Read XML File NodeSystem.Xml.XmlReader System.Xml.XmlReaderSettings System.Xml.XmlNodeType
;*************************************************************************** ;** ReadXmlFile Sample ;** ;** Purpose: Read XML file Node by Node ;** Inputs: ;** Outputs: ;** Reference: ;** REQUIRES WinBatch 2013A or newer ;** ;** Developer: Deana Falk 2013.08.07 ;*************************************************************************** If Version( )< '2013A' Pause('Notice', 'Need 2013A or Newer Version of WinBatch') Exit EndIf GoSub UDFS fn = AskFilename( 'ReadXmlFile', 'c:\temp\Data\XML\', '', '', 1 ) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Load assemblies into the WinBatch process ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded. ObjectClrOption ("useany","System.Xml") ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Create a class implemented by a managed assembly. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ netXmlReader = ObjectClrNew("System.Xml.XmlReader") netXmlSettings = ObjectClrNew("System.Xml.XmlReaderSettings") enumXmlNodeType = ObjectClrNew("System.Xml.XmlNodeType") ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Associate a Framework based type name with a value ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;bTrue = ObjectCLRType("System.Boolean",true) ;Pause('',ObjectTypeGet(type) ) bTrue = ObjectType( "BOOL", -1 ) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Set Setting properties ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;netXmlReader.Async = bTrue ;netXmlSettings.IgnoreComments = bTrue ;netXmlSettings.IgnoreProcessingInstructions = bTrue netXmlSettings.IgnoreWhitespace = bTrue ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Call Create Method ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;r = netXmlReader.Create(fn, System.Xml.XmlReaderSettings:netXmlSettings) r = netXmlReader.Create(fn, netXmlSettings) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Call method. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BoxesUp("100,100,900,900", @NORMAL) BoxCaption(1, "Inspect XML Nodes") BoxTextFont(1, "Courier New", 20, 0, 16) BoxDrawRect(1, '0,0,1000,1000', 0 ) While r.Read() BoxDataTag(1, "TAG1") arrRslt = udfInspectCurrentNode(r) data = 'Name: ':@TAB:arrRslt[0]:@LF:'LocalName: ':@TAB:arrRslt[1]:@LF:'NamespaceURI: ':@TAB:arrRslt[2]:@LF:'NodeType: ':@TAB:arrRslt[3]:@LF:'HasValue: ':@TAB:arrRslt[4]:@LF:'Value: ':@TAB:arrRslt[5]:@LF:'HasAttributes: ':@TAB:arrRslt[6]:@LF:'AttributeCount:':@TAB:arrRslt[7] BoxDrawText(1, '0,0,1000,1000', data, @TRUE, 0) TimeDelay(0.5) BoxDataClear(1, "TAG1") EndWhile Exit :UDFS #DefineFunction udfInspectCurrentNode(netXmlReader) arrResult = ArrDimension(8) arrResult[0] = netXmlReader.Name arrResult[1] = netXmlReader.LocalName arrResult[2] = netXmlReader.NamespaceURI arrResult[3] = udfXmlNodeTypeToString(netXmlReader.NodeType) arrResult[4] = netXmlReader.HasValue arrResult[5] = netXmlReader.Value arrResult[6] = netXmlReader.HasAttributes arrResult[7] = netXmlReader.AttributeCount Return arrResult #EndFunction #DefineFunction udfXmlNodeTypeToString(nType) Switch nType ; This is returned by the XmlReader if a Read method has not been called. Case 0 sType = 'NONE' Break ; Element An element (for example, <item> ). ; An Element node can have the following child node types: Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference. It can be the child of the Document, DocumentFragment, EntityReference, and Element nodes. Case 1 sType = 'Element' Break ; Attribute An attribute (for example, id='123' ). ; An Attribute node can have the following child node types: Text and EntityReference. The Attribute node does not appear as the child node of any other node type. It is not considered a child node of an Element. Case 2 sType = 'Attribute' Break ; Text The text content of a node. ; A Text node cannot have any child nodes. It can appear as the child node of the Attribute, DocumentFragment, Element, and EntityReference nodes. Case 3 sType = 'Text' Break ; CDATA A CDATA section (for example, <![CDATA[my escaped text]]> ). ; CDATA sections are used to escape blocks of text that would otherwise be recognized as markup. A CDATA node cannot have any child nodes. It can appear as the child of the DocumentFragment, EntityReference, and Element nodes. Case 4 sType = 'CDATA' Break ; EntityReference A reference to an entity (for example, # ). ; An EntityReference node can have the following child node types: Element, ProcessingInstruction, Comment, Text, CDATA, and EntityReference. It can appear as the child of the Attribute, DocumentFragment, Element, and EntityReference nodes. Case 5 sType = 'EntityReference' Break ; Entity An entity declaration (for example, <!ENTITY...> ). ; An Entity node can have child nodes that represent the expanded entity (for example, Text and EntityReference nodes). It can appear as the child of the DocumentType node. Case 6 sType = 'Entity' Break ; ProcessingInstruction A processing instruction (for example, <?pi test?> ). ; A ProcessingInstruction node cannot have any child nodes. It can appear as the child of the Document, DocumentFragment, Element, and EntityReference nodes. Case 7 sType = 'ProcessingInstruction' Break ; Comment A comment (for example, <!-- my comment --> ). ; A Comment node cannot have any child nodes. It can appear as the child of the Document, DocumentFragment, Element, and EntityReference nodes. Case 8 sType = 'Comment' Break ; Document A document object that, as the root of the document tree, provides access to the entire XML document. ; A Document node can have the following child node types: XmlDeclaration, Element (maximum of one), ProcessingInstruction, Comment, and DocumentType. It cannot appear as the child of any node types. Case 9 sType = 'Document' Break ; DocumentType The document type declaration, indicated by the following tag (for example, <!DOCTYPE...> ). ; A DocumentType node can have the following child node types: Notation and Entity. It can appear as the child of the Document node. Case 10 sType = 'DocumentType' Break ; DocumentFragment A document fragment. ; The DocumentFragment node associates a node or subtree with a document without actually being contained within the document. A DocumentFragment node can have the following child node types: Element, ProcessingInstruction, Comment, Text, CDATA, and EntityReference. It cannot appear as the child of any node types. Case 11 sType = 'DocumentFragment' Break ; Notation A notation in the document type declaration (for example, <!NOTATION...> ). ; A Notation node cannot have any child nodes. It can appear as the child of the DocumentType node. Case 12 sType = 'Notation' Break ; Whitespace White space between markup. Case 13 sType = 'Whitespace' Break ; SignificantWhitespace White space between markup in a mixed content model or white space within the xml:space="preserve" scope. Case 14 sType = 'SignificantWhitespace' Break ; EndElement An end element tag (for example, </item> ). ; EndElement nodes are returned when XmlReader gets to the end of an element. Case 15 sType = 'EndElement' Break ; EndEntity ; Returned when XmlReader gets to the end of the entity replacement as a result of a call to ResolveEntity. Case 16 sType = 'EndEntity' Break ; XmlDeclaration ; The XML declaration (for example, <?xml version='1.0'?> ). ; The XmlDeclaration node must be the first node in the document. It cannot have children. It is a child of the Document node. It can have attributes that provide version and encoding information. Case 17 sType = 'XmlDeclaration' Break EndSwitch Return sType #EndFunction Return
Article ID: W17851
Filename: ReadXmlFile Sample.txt
File Created: 2023:05:31:11:14:16
Last Updated: 2023:05:31:11:14:16