System.Xml
According to Microsoft’s link https://msdn.microsoft.com/en-us/library/system.xml(v=vs.110).aspx, this namespace provides standards base for processing XML.
An important fact to know in Dynamo is that the version that it uses is IronPython (2.7x). While this may provide some disadvantages because a newer version of python would allow you to load numpy or other great python pacalges, there are however some advantages in having the .NET framework at hand.
System.Xml is a assembly that can be loaded but it needs referencing. This can be easily done by using clr.AddReference.
import clr import System clr.AddReference("System.Xml") from System import Xml
There are some other python packages you can use to read xml files, and one search with google will send to to the etree package. However, it’s not as easy to use as System.Xml.
Imaging you have the following the 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>
Properties and Methods
Important properties and methods are :
XmlDocument – will load the xml document and parse it to it’s constituent elements.
XmlElement – the document or the element have ChildNodes. Those nodes that are element type can be cast(ed) to this type.
ChildNodes – returns all nodes of an element. This can be a text, a coment , an element, etc.
NodeType – you can see what type of node you have by accessing this property. This will provide on value from the enum XmlNodeType. A node is an element if this value is Element or EndElement.
GetElementByTagName(string name) – A usefull method allows you to pass in a tag name (like Person) and to get all elements with this tag. This method returns an XmlNodeList, this is just an IEnumerable (like an array) of nodes that you can iterate using ‘for node in returnedNodeList‘.
You can get all attributes of an element using the property Attributes or you can extract an attribute using the method GetAttribute(String) that accepts the name of the attribute as argument
Example
# Enable Python support and load DesignScript library import clr import System clr.AddReference("System.Xml") from System import Xml #create a new XmlDocument xmlDocument = Xml.XmlDocument() #loading an xml document from path xmlDocument.Load(IN[0]) '''Get the root element ''' root = xmlDocument.DocumentElement '''Get the persons tag using the method GetElementsByTagName''' persons = root.GetElementsByTagName("Persons")[0] '''Or using the string indexer like''' cities = root['Cities'] '''Get the child nodes of persons''' personNodes = persons.ChildNodes '''getting the child node of type comment''' for node in personNodes: if node.NodeType == Xml.XmlNodeType.Comment: comment = node.InnerText '''getting the first child (#first person)''' firstPerson = persons.GetElementsByTagName("Person")[0] '''getting an attribute value''' firstPersonName = firstPerson.GetAttribute('Name') OUT = root.InnerXml,\ persons.InnerXml,\ cities.InnerXml,\ comment,\ firstPersonName