При добавлении узла в XML генерируется NullReferenceException - C#
Формулировка задачи:
Доброго для. Пытаюсь добавить новый элемент в xml файл, что что-то происходит не так, не могу понять.
Вот сам код добавления элемента
Метод вызывается тут
В чём проблема? Раньше добавлял данные без проблем, что сей час не так?
NullReferenceException: Object reference not set to an instance of an object TooltipXml.addElementXml (System.String lg, System.String ID, System.String toolTip, System.String txt) (at Assets/Scripts/TooltipXml.cs:52) TooltipXml.EditElementXmlToolTip (System.String ID, System.String toolTipText, System.String local, System.String xmlPath) (at Assets/Scripts/TooltipXml.cs:93) TooltipManager.CreateWindow () (at Assets/Scripts/TooltipManager.cs:203) TooltipManager.OnGUI () (at Assets/Scripts/TooltipManager.cs:141)
//добавление элемента
public void addElementXml(string lg, string ID, string toolTip, string txt)
{
XmlElement xRoot = xDoc.DocumentElement;
// создаем новый элемент
XmlElement element = xDoc.CreateElement("element");
// создаем атрибуты
XmlAttribute idAttrEl = xDoc.CreateAttribute("id");
// создаем элемент localization
XmlElement localizationElem = xDoc.CreateElement("localization");
// создаем атрибуты для localization
XmlAttribute langAttr = xDoc.CreateAttribute("lang");
XmlAttribute tooltipgAttr = xDoc.CreateAttribute("tooltip");
XmlAttribute textAttr = xDoc.CreateAttribute("text");
// создаем текстовые значения для элементов и атрибутов
XmlText id = xDoc.CreateTextNode(ID);
XmlText lang = xDoc.CreateTextNode(lg);
XmlText tooltipText = xDoc.CreateTextNode(toolTip);
XmlText text = xDoc.CreateTextNode(txt);
//добавляем узлы и атрибуты
idAttrEl.AppendChild(id);
element.Attributes.Append(idAttrEl);
langAttr.AppendChild(lang);
localizationElem.Attributes.Append(langAttr);
tooltipgAttr.AppendChild(tooltipText);
localizationElem.Attributes.Append(tooltipgAttr);
textAttr.AppendChild(text);
localizationElem.Attributes.Append(textAttr);
//localizationElem.AppendChild(tooltipText);
element.AppendChild(localizationElem);
xRoot.AppendChild(element);
} public void EditElementXmlToolTip(string ID, string toolTipText, string local, string xmlPath)
{
string pathX = @"/root/element[@id='" + ID + @"']/localization[@lang='" + local + @"']";
string path_id = @"/root/element[@id='" + ID + @"']";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
if (xmlDoc.SelectSingleNode(path_id) != null && xmlDoc.SelectSingleNode(pathX) != null)
{
Debug.Log("yes");
XmlNode localizationElement = xmlDoc.SelectSingleNode(pathX);
localizationElement.Attributes["tooltip"].Value = toolTipText;
xmlDoc.Save(xmlPath);
}
else
{
Debug.Log("no, so create new element of xml");
addElementXml(local, ID, toolTipText, ""); // это он
xmlDoc.Save(xmlPath);
}Решение задачи: «При добавлении узла в XML генерируется NullReferenceException»
textual
Листинг программы
public void addElementXml(string lg, string ID, string toolTip, string txt)
{
XmlElement element = xDoc.CreateElement("element");
element.SetAttribute("id", ID);
XmlElement localizationElem = xDoc.CreateElement("localization");
localizationElem.SetAttribute("lang", lg);
localizationElem.SetAttribute("tooltip", toolTip);
localizationElem.SetAttribute("text", txt);
element.AppendChild(localizationElem);
xDoc.DocumentElement.AppendChild(element);
}