Mark O'Reilly · Apr 9, 2020

How to get value from %XML.Text reader

I'm having trouble trying to navigate XML files USING the %XML.TextReader. I have read
but I still don't really understand. 


I have the following block 

while textreader.Read()
    set practices= practices_textreader.Name_":"
        If textreader.Value'="" 
            set practices= practices_textreader.Value_" "
            elseset practices= practices_"NOVALUE" _" "}
        }         write practices

The xml for all intents and purposes is 



The output i get is 

NACS:NOVALUE     :111111    

Why is the value not attached to the textreader.Name for the name NACS. The XML is valid xml from an api call.


0 407
Discussion (2)3
Log in or sign up to continue

%XML.TextReader traverses XML nodes. Each node can have different type. NACS is node of type Element. It does not have any value. Node 111111 is of type Chars. However it does not have any name. Thus the output that you see.

Hint: change line in your code as follows to see types of nodes :

set practices = practices_textreader.Name_"("_textreader.NodeType_"):"

Take a look here for data model of XML:

See also class reference for %XML.TextReader. Particularly for Value property. It explains what this property holds depending on type of node.

The code below does want you want.

    While textreader.Read() {
        If textreader.NodeType = "element"  {  
            Set elementName=textreader.Name  
        ElseIf textreader.NodeType = "chars" {
            Set elementValue=textreader.Value
            Set practices=practices_elementName_":"_elementValue_" "
    Write practices