<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Editing User.config creates a corrupted file, even with valid XML in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/editing-user-config-creates-a-corrupted-file-even/m-p/1481657#M70693</link>
    <description>&lt;P&gt;So, as a follow up to this post:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/set-pro-s-default-settings-programmatically/m-p/1476524" target="_blank"&gt;https://community.esri.com/t5/arcgis-pro-questions/set-pro-s-default-settings-programmatically/m-p/1476524&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I've been trying to edit user.config. I'm having no problem editing nodes that already exist in the config file.&lt;/P&gt;&lt;P&gt;Here's where I'm having trouble:&lt;/P&gt;&lt;P&gt;If the node that I want (e.g.&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp;RequireExplicitStartEditing&lt;/EM&gt;&lt;/STRONG&gt;) isn't there, I make a new one&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;    def newTag(tree, tag, section, value, serialType): 
               #XML Tree Object, 
               #"RequireExplicitStartEditing",
               #"Editing", 
               #"True", 
               #"String")
        
        parEl = tree.find(f"./userSettings/ArcGIS.Desktop.{section}.Settings")
        createFols = ET.SubElement(parEl,
                                  "setting", 
                                  attrib = {"name": tag, 
                                           "serialAs":serialType})
                                           
        child = ET.SubElement(createFols, "value")
        child.text = value
        return&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works great. It creates valid XML and no Python errors.&lt;/P&gt;&lt;P&gt;The issue I'm having is that opening up Pro after this will either cause it to crash OR give me the following:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1717109049451.png" style="width: 400px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="AlfredBaldenweck_0-1717109049451.png" style="width: 375px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105840i669BA12FF71BEC80/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1717109049451.png" alt="AlfredBaldenweck_0-1717109049451.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;If I click "Yes", I get this message, which destroys the config file and undoes my work.&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1717109080724.png" style="width: 400px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="AlfredBaldenweck_1-1717109080724.png" style="width: 375px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105841i3146D9AC6A76D833/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1717109080724.png" alt="AlfredBaldenweck_1-1717109080724.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;So, what am I doing wrong?&lt;/STRONG&gt; I'd like to be able to create the necessary settings nodes on a fresh config file for each user.&lt;/P&gt;&lt;P&gt;Again, if the node is already there, I can edit the values no problem and have it work (not including my code for this one). It's when they're new that I have issues.&lt;/P&gt;&lt;P&gt;I've confirmed that the sections match up correctly in the configSections node (you need to add two sections for editing) and played with the node order, and I just can't get it to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 May 2024 06:31:20 GMT</pubDate>
    <dc:creator>ShareUser</dc:creator>
    <dc:date>2024-05-31T06:31:20Z</dc:date>
    <item>
      <title>Editing User.config creates a corrupted file, even with valid XML</title>
      <link>https://community.esri.com/t5/python-questions/editing-user-config-creates-a-corrupted-file-even/m-p/1481657#M70693</link>
      <description>&lt;P&gt;So, as a follow up to this post:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/set-pro-s-default-settings-programmatically/m-p/1476524" target="_blank"&gt;https://community.esri.com/t5/arcgis-pro-questions/set-pro-s-default-settings-programmatically/m-p/1476524&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I've been trying to edit user.config. I'm having no problem editing nodes that already exist in the config file.&lt;/P&gt;&lt;P&gt;Here's where I'm having trouble:&lt;/P&gt;&lt;P&gt;If the node that I want (e.g.&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp;RequireExplicitStartEditing&lt;/EM&gt;&lt;/STRONG&gt;) isn't there, I make a new one&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;    def newTag(tree, tag, section, value, serialType): 
               #XML Tree Object, 
               #"RequireExplicitStartEditing",
               #"Editing", 
               #"True", 
               #"String")
        
        parEl = tree.find(f"./userSettings/ArcGIS.Desktop.{section}.Settings")
        createFols = ET.SubElement(parEl,
                                  "setting", 
                                  attrib = {"name": tag, 
                                           "serialAs":serialType})
                                           
        child = ET.SubElement(createFols, "value")
        child.text = value
        return&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works great. It creates valid XML and no Python errors.&lt;/P&gt;&lt;P&gt;The issue I'm having is that opening up Pro after this will either cause it to crash OR give me the following:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1717109049451.png" style="width: 400px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="AlfredBaldenweck_0-1717109049451.png" style="width: 375px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105840i669BA12FF71BEC80/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1717109049451.png" alt="AlfredBaldenweck_0-1717109049451.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;If I click "Yes", I get this message, which destroys the config file and undoes my work.&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1717109080724.png" style="width: 400px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="AlfredBaldenweck_1-1717109080724.png" style="width: 375px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105841i3146D9AC6A76D833/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1717109080724.png" alt="AlfredBaldenweck_1-1717109080724.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;So, what am I doing wrong?&lt;/STRONG&gt; I'd like to be able to create the necessary settings nodes on a fresh config file for each user.&lt;/P&gt;&lt;P&gt;Again, if the node is already there, I can edit the values no problem and have it work (not including my code for this one). It's when they're new that I have issues.&lt;/P&gt;&lt;P&gt;I've confirmed that the sections match up correctly in the configSections node (you need to add two sections for editing) and played with the node order, and I just can't get it to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2024 06:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/editing-user-config-creates-a-corrupted-file-even/m-p/1481657#M70693</guid>
      <dc:creator>ShareUser</dc:creator>
      <dc:date>2024-05-31T06:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Editing User.config creates a corrupted file, even with valid XML</title>
      <link>https://community.esri.com/t5/python-questions/editing-user-config-creates-a-corrupted-file-even/m-p/1485339#M70724</link>
      <description>&lt;P&gt;Have you run a diff on the original config and the new config? I just tried it with this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import xml.etree.ElementTree as ElementTree

def newTag(tree: ElementTree, tag: str, section: str, value: ..., serialType: str = "String"):
    """Create a new tag in the XML tree object.
    Args:
        tree (ElementTree): The XML tree object.
        tag (str): The name of the tag.
        section (str): The section of the XML tree object.
        value (str): The value of the tag.
        serialType (str): The serial type of the tag. Defaults to "String".
    """
    parEl: ElementTree.Element = tree.find(f"./userSettings/ArcGIS.Desktop.{section}.Settings")
    createFols = ElementTree.SubElement(parEl,
                              "setting", 
                              attrib = {"name": tag, 
                                       "serialAs":serialType})
    child = ElementTree.SubElement(createFols, "value")
    child.text = value
    return

if __name__ == "__main__":
    tree = ElementTree.parse(r"&amp;lt;old_config&amp;gt;")
    newTag(tree, "RequireExplicitStartEditing", "Editing", "True")
    tree.write(r"&amp;lt;new_config&amp;gt;")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the output XML was missing the xml version tag and the xmlns:xsi property (Generated on Right, Original on Left):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HaydenWelch_0-1717593529725.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/106235i699CD27EEF471884/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HaydenWelch_0-1717593529725.png" alt="HaydenWelch_0-1717593529725.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Those were the only differences between the files beyond some slight whitespace and line formatting that shouldn't effect anything&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 13:22:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/editing-user-config-creates-a-corrupted-file-even/m-p/1485339#M70724</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-06-05T13:22:02Z</dc:date>
    </item>
  </channel>
</rss>

