Добавление данных в XML - C#
Формулировка задачи:
Здравствуйте.
Есть такой XML-документ:
Мне необходимо добавить
Написал следующее:
получил ноду в которой могу прочитать все значения по имени.
Но как добавить ещё значение?
Пробовал
но это не дало необходимого результата.
Прошу помощи в реализации.
Заранее благодарен.
Запись:
<?xml version="1.0" encoding="utf-8" ?> - <UICustomizationSet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DefaultIcon>AvevaSharedIcons:ID_WARNING</DefaultIcon> - <UICustomizationFiles> <CustomizationFile Name="Module" Path="design.uic" /> <CustomizationFile Name="SchematicExplorerAddin" Path="CoreSchematicMenu.uic" /> <CustomizationFile Name="Project" Path="$1.uic" Optional="true" /> <CustomizationFile Name="SVGCompare" Path="SVGCompare.uic" /> <CustomizationFile Name="Cabling" Path="AVEVA.design.cabling.uic" /> <CustomizationFile Name="Hvac" Path="AVEVA.design.hvac.uic" /> <CustomizationFile Name="Supports" Path="AVEVA.design.MDS.uic" /> <CustomizationFile Name="Piping" Path="AVEVA.design.piping.uic" /> <CustomizationFile Name="Steelwork" Path="AVEVA.design.steelwork.uic" /> <CustomizationFile Name="MessageAddin" Path="MessageWindowCoreMenus.uic" /> <CustomizationFile Name="Laser" Path="AVEVA.design.laser.uic" /> <CustomizationFile Name="Integrator" Path="Integrator.uic" /> <CustomizationFile Name="DiagramViewer" Path="DiagramViewer.uic" /> <CustomizationFile Name="InstrumentationImportAddin" Path="InstrumentationImportAddin.uic" /> <CustomizationFile Name="CAD Cable SYSTEM" Path="CADCableSystems.uic" /> <CustomizationFile Name="CAD Piping" Path="CADPiping.uic" /> </UICustomizationFiles> </UICustomizationSet>
<CustomizationFile Name="Test Zone" Path="TestZone.uic" />
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(customXMLFileName);
var node = xmlDoc.DocumentElement.SelectSingleNode("/UICustomizationSet/UICustomizationFiles");foreach (XmlNode item in node.ChildNodes)
{
MessageBox.Show(item.Attributes.GetNamedItem("Name").Value);
} XmlElement elem = xmlDoc.CreateNode( ( "CustomizationFile");
elem.InnerText = "Name = "Test Zone" Path = "TestZone.uic"";
В общем покопался по ссылкам и нашёл здесь
Думаю разберётесь что к чему (методы из реального кода, всё работает как надо.)
Чтение:
public bool FindCustomizationInfo(string Name)
{
bool result = false;
string customXMLFileName = configData.AvevaPath + configData.E3DFolder + "" + configData.DesignCustomization;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(customXMLFileName);
var node = xmlDoc.DocumentElement.SelectSingleNode("/UICustomizationSet/UICustomizationFiles");
foreach (XmlNode item in node.ChildNodes)
{
if (Name == item.Attributes.GetNamedItem("Name").Value) { result = true; break; }
}
return result;
} public void AddCustomizationInfo(string Name, string Path)
{
string customXMLFileName = configData.AvevaPath + configData.E3DFolder + "" + configData.DesignCustomization;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(customXMLFileName);
var node = xmlDoc.DocumentElement.SelectSingleNode("/UICustomizationSet/UICustomizationFiles");
XmlNode elem = xmlDoc.CreateElement("CustomizationFile");
XmlAttribute xmlAttrName = xmlDoc.CreateAttribute("Name");
xmlAttrName.Value = Name;
elem.Attributes.Append(xmlAttrName);
XmlAttribute xmlAttrPath = xmlDoc.CreateAttribute("Path");
xmlAttrPath.Value = Path;
elem.Attributes.Append(xmlAttrName);
elem.Attributes.Append(xmlAttrPath);
node.AppendChild(elem);
xmlDoc.Save(customXMLFileName);
}Решение задачи: «Добавление данных в XML»
textual
Листинг программы
<UICustomizationSet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DefaultIcon>AvevaSharedIcons:ID_WARNING</DefaultIcon> <UICustomizationFiles> <CustomizationFile Name="Module" Path="design.uic" /> <CustomizationFile Name="SchematicExplorerAddin" Path="CoreSchematicMenu.uic" /> <CustomizationFile Name="Project" Path="$1.uic" Optional="true" /> <CustomizationFile Name="SVGCompare" Path="SVGCompare.uic" /> <CustomizationFile Name="Cabling" Path="AVEVA.design.cabling.uic" /> <CustomizationFile Name="Hvac" Path="AVEVA.design.hvac.uic" /> <CustomizationFile Name="Supports" Path="AVEVA.design.MDS.uic" /> <CustomizationFile Name="Piping" Path="AVEVA.design.piping.uic" /> <CustomizationFile Name="Steelwork" Path="AVEVA.design.steelwork.uic" /> <CustomizationFile Name="MessageAddin" Path="MessageWindowCoreMenus.uic" /> <CustomizationFile Name="Laser" Path="AVEVA.design.laser.uic" /> <CustomizationFile Name="Integrator" Path="Integrator.uic" /> <CustomizationFile Name="DiagramViewer" Path="DiagramViewer.uic" /> <CustomizationFile Name="InstrumentationImportAddin" Path="InstrumentationImportAddin.uic" /> <CustomizationFile Name="CAD Cable SYSTEM" Path="CADCableSystems.uic" /> <CustomizationFile Name="CAD Piping" Path="CADPiping.uic" /> <CustomizationFile Name="Test Zone" Path="TestZone.uic" /> </UICustomizationFiles> </UICustomizationSet>