Reading xml file with python in Dynamo — Digitteck
Reading xml file with python in Dynamo
dotnet·24 June 2018·3 min read

Reading xml file with python in Dynamo

Dynamo uses IronPython 2.7, which gives you access to the full .NET framework alongside Python syntax. System.Xml is the .NET namespace for standards-based XML processing and is far easier to use inside Dynamo than Python's xml.etree module. Load it with clr.AddReference:

python
import clr
import System
clr.AddReference("System.Xml")
from System import Xml

Properties and Methods

  • XmlDocument — loads and parses an XML file into its constituent elements.
  • DocumentElement — returns the root element of the document.
  • ChildNodes — returns all child nodes of an element (text, comments, elements, etc.).
  • NodeType — identifies the node's type via the XmlNodeType enum.
  • GetElementsByTagName(name) — returns an XmlNodeList of all elements with that tag; iterate with for node in list.
  • Attributes / GetAttribute(name) — access element attributes by name.
  • string indexer (element['TagName']) — shorthand to get the first direct child element with that tag.

Example XML Document

xml
<root>
    <Persons>
        <Person Name="Doe" FirstName="John">
            <Country>Romania</Country>
            <City>100</City>
        </Person>
    </Persons>

    <Cities>
        <City Name="London" Id="100"/>
        <City Name="Torino" Id="101"/>
        <City Name="Berlin" Id="102"/>
        <City Name="Bucharest" Id="103"/>
    </Cities>
</root>

Example

python
# Enable Python support and load DesignScript library
import clr
import System
clr.AddReference("System.Xml")
from System import Xml

# Create and load XmlDocument from file path (IN[0])
xmlDocument = Xml.XmlDocument()
xmlDocument.Load(IN[0])

# Get the root element
root = xmlDocument.DocumentElement

# GetElementsByTagName returns an XmlNodeList — iterate with 'for node in list'
persons = root.GetElementsByTagName("Persons")[0]

# String indexer is equivalent to GetElementsByTagName for direct children
cities = root['Cities']

# Iterate child nodes and filter by type
personNodes = persons.ChildNodes
comment = None
for node in personNodes:
    if node.NodeType == Xml.XmlNodeType.Comment:
        comment = node.InnerText

# Access a specific element by tag and read its attribute
firstPerson = persons.GetElementsByTagName("Person")[0]
firstPersonName = firstPerson.GetAttribute('Name')

OUT = root.InnerXml, persons.InnerXml, cities.InnerXml, comment, firstPersonName

Tags

DynamoPython.NETXML
digitteck

© 2026 Digitteck