Я вызываю скрипт на Python из C #, заставляя его работать на оболочке.Код Python обновляет XML.Работает нормально при запуске из оболочки python, внешне.Но когда он запускается через c #, он обновляется при запуске скрипта, так как я вижу изменения, происходящие в XML с помощью операторов печати, но когда код завершен, файл XML все еще не поврежден.
Я напечатал значения из xml, до обновления и после обновления.И обновление происходит.Но когда я открываю XML-файл в редакторе, никаких изменений не происходит.Как это исправить?
C #:
private static void doPython()
{
string fileName =@"I:\Python\XMLParser\BW_Config_ConfigureCode.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\Users\suddin\AppData\Local\Programs\Python\Python37-32\python.exe", fileName)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
}
Python: import xml.etree.ElementTree as ET
# Welcome! In this file, we configure all the tags of BW_Config.xml, which are there in the file BW_Config_Installer_Configurations.xml
# We parse 2 xml files in this code.
# First is the BW_Config.xml which we need to configure
# Second is the BW_Config_Installer_Configurtion.xml which contains the information about which tags are to be configured in the BW_Config.xml
BW_Config_tree = ET.parse('I:\Python\XMLParser\BW_Config_for_parsing_practice.xml') # parsed BW_Config.xml
BW_Config_root = BW_Config_tree.getroot()
print(BW_Config_root.tag)
# parsed BW_Config_Installer_Configurations.xml
installer_config_tree =ET.parse('I:\Python\XMLParser\BW_Config_Installer_Configurations.xml')
installer_config_root = installer_config_tree.getroot()
print(installer_config_root.tag)
i = 0
# This loop will traverse all the tags which need to be cofigured, one by one
for configurable_tag in installer_config_root.findall("./BW_Config/"):
# This variable will contain the name of the tag to be configured in each iteration
configurable_tagname = configurable_tag.tag
# This variable will contain the path of the tag inside BW_Config.xml, in each iteration
configurable_tagpath = configurable_tag.attrib["xpath"]
# This variable will contain the data about what is to be configure in the tag, in each iteration
configurable_tagattribute = configurable_tag.attrib["property"]
# This variable will be set to "yes", if the respective tag is to be changed, else "no"
configurable_tagchoice = configurable_tag.attrib["configurable"]
# This variable will contain the new value for the respective tag
configurable_tagnewvalue = configurable_tag.attrib["value"]
i = i + 1 # It is used just to maintain the number of iterations and for user friendly console display
# Printing which tag we will be dealing with in this iteration
print(i, configurable_tagname)
print("\n")
if configurable_tagchoice=="yes":
if configurable_tagattribute != "text":
for elem in BW_Config_root.findall(configurable_tagpath):
for name, value in elem.attrib.items():
print(name, ":", value)
elem.attrib[configurable_tagattribute] = configurable_tagnewvalue
BW_Config_tree.write("BW_Config_for_parsing_practice.xml")
print("-------\nUpdated\n-------")
for name, value in elem.attrib.items():
print(name, ":", value)
print("\n")
else:
print("Text is to be configured")
for elem in BW_Config_root.findall(configurable_tagpath):
if len(elem.attrib) == 0:
print("There are no attributes to the tag "+elem.tag)
print("Text inside the tag is \""+str(elem.text)+"\"")
else:
for name, value in elem.attrib.items():
print(name, ":", value)
elem.text=configurable_tagnewvalue
BW_Config_tree.write("BW_Config_for_parsing_practice.xml")
print("-------\nUpdated\n-------")
print("New Text inside tag: "+elem.text)
else:
print("To Configure, set the attribute \"configurable\" to \"yes\"")
Я получаю эту ошибку: