LINQ не дает добавить новый узел в xml файл - C#
Формулировка задачи:
Пытаюсь добавить узел между элементами xml, но что-то видно делаю не так. Пытаюсь найти нужный учел и в него добавить новый. Делаю следующим образом:
xmlPath - путь к файлу. Программа ругается на параметр "First", пытался заменить на "Where", но та же песня.
Лог выдаёт следующее:
Что не так?
Заранее спасибо.
Так как понял, что XElement.Parse ест xml строку, но проблема та же. Так же вопрос, как тогда передать в XElement.Parse весь файл xml?
XElement doc = XElement.Parse(xmlPath);
doc.Descendants("element")
.First(e => e.Attribute("id").Value == ID)
.Add(new XElement("localization", new XAttribute("lang", local),
new XAttribute("tooltip", toolTipText),
new XAttribute("text", " ")));Assets/Scripts/TooltipXml.cs(142,42): error CS1061: Type `System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for `First' and no extension method `First' of type `System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)
Я попробовал сделать так:
XElement doc = XElement.Parse(@"<?xml version='1.0' encoding='utf - 8'?>
<root>
<element id = '2'>
<localization lang = 'en' tooltip = 'Cube in cube №2' text = '' />
</element>
</root> ");
doc.Descendants("element")
.First(e => e.Attribute("id").Value == ID)
.Add(new XElement("localization", new XAttribute("lang", local),
new XAttribute("tooltip", toolTipText),
new XAttribute("text", " ")));Решение задачи: «LINQ не дает добавить новый узел в xml файл»
textual
Листинг программы
XDocument x = XDocument.Load(xmlPath);
x.Descendants("element")
.First(e => e.Attribute("id").Value == ID)
.Add(
new XElement("localization",
new XAttribute("lang", local),
new XAttribute("tooltip", toolTipText),
new XAttribute("text", " ")
));