def write_ioc(root, output_dir=None, force=False):
root_tag = '<STR_LIT>'<EOL>if not force and root.tag != root_tag:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(root_tag))<EOL><DEDENT>default_encoding = '<STR_LIT:utf-8>'<EOL>tree = root.getroottree()<EOL>try:<EOL><INDENT>encoding = tree.docinfo.encoding<EOL><DEDENT>except:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>encoding = default_encoding<EOL><DEDENT>ioc_id = root.attrib['<STR_LIT:id>']<EOL>fn = ioc_id + '<STR_LIT>'<EOL>if output_dir:<EOL><INDENT>fn = os.path.join(output_dir, fn)<EOL><DEDENT>else:<EOL><INDENT>fn = os.path.join(os.getcwd(), fn)<EOL><DEDENT>try:<EOL><INDENT>with open(fn, '<STR_LIT:wb>') as fout:<EOL><INDENT>fout.write(et.tostring(tree, encoding=encoding, xml_declaration=True, pretty_print=True))<EOL><DEDENT><DEDENT>except (IOError, OSError):<EOL><INDENT>log.exception('<STR_LIT>')<EOL>return False<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>return True<EOL>
Serialize an IOC, as defined by a set of etree Elements, to a .IOC file.

:param root: etree Element to write out.  Should have the tag 'OpenIOC'
:param output_dir: Directory to write the ioc out to.  default is current working directory.
:param force: If set, skip the root node tag check.
:return: True, unless an error occurs while writing the IOC.