Мне нужен код на c#. Эта процедура в которую передается (string Param, string Value).…
- User
Мне нужен код на c#. Эта процедура в которую передается (string Param, string Value). Входящие параметры записываются в XML файл где лежит exe файл приложения. Вторая процедура читает эти значения и возвращает тип string. Но имеет всего входной параметр типа стринг по которому находит нужный параметр для чтения.
- Assistant
- User
public static void WriteParamValue(string Param, string Value) { string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
string xmlPath = Path.ChangeExtension(exePath, "xml"); XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(xmlPath)) { xmlDoc.Load(xmlPath); } else { XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF - 8", null); XmlElement root = xmlDoc.DocumentElement; xmlDoc.InsertBefore(xmlDeclaration, root); } XmlElement paramElement = xmlDoc.CreateElement("Param"); paramElement.SetAttribute("Name", Param); paramElement.InnerText = Value; xmlDoc.DocumentElement.AppendChild(paramElement); xmlDoc.Save(xmlPath); } jib,rf dcnhjrt - Assistant
Ошибка в данном коде связана с использованием недопустимого символа при создании
XmlDeclaration. Замените"UTF - 8"на"UTF-8"(без пробела) в следующей строке:XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF - 8", null);Исправленный код будет выглядеть следующим образом:
public static void WriteParamValue(string Param, string Value) { string exePath = System.Reflection.Assembly.GetEntryAssembly().Location; string xmlPath = Path.ChangeExtension(exePath, "xml"); XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(xmlPath)) { xmlDoc.Load(xmlPath); } else { XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = xmlDoc.DocumentElement; xmlDoc.InsertBefore(xmlDeclaration, root); } XmlElement paramElement = xmlDoc.CreateElement("Param"); paramElement.SetAttribute("Name", Param); paramElement.InnerText = Value; xmlDoc.DocumentElement.AppendChild(paramElement); xmlDoc.Save(xmlPath); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.