我正在通过使C#在外壳上运行来从C#调用python脚本。 python代码更新XML。从外部从python shell运行时,它工作正常。但是,当通过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: 将xml.etree.ElementTree导入为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\"")
我收到此错误: