XML System.InvalidOperationException: "Повторяющийся атрибут."
Кто может растолковать ошибку и помочь исправить, вылетает в строке
tagDocument.Add(tagData, new XAttribute("СТРОКА", $"{countofStroka:d3}"))
после второй иттерации
foreach (var item in countIterations)
{
XElement tagDocument = new XElement("Document", new XAttribute("ПлСч11", item));
XElement tagData = new XElement("Data");
xDoc.Root.Add(tagDocument); //<Document ПлСч11="120111000">
int countofStroka = 1;
for (int i = 0; i < 2; i++)
{
tagDocument.Add(tagData, new XAttribute("СТРОКА", $"{countofStroka:d3}"));
tagData.Add(new XElement("Px", new XAttribute("Value", $"{docs.DocumentList[i].AccountNumber}"), new XAttribute("Num", $"1")));
tagData.Add(new XElement("Px", new XAttribute("Value", $"{docs.DocumentList[i].Beginning_balance}"), new XAttribute("Num", $"2")));
tagData.Add(new XElement("Px", new XAttribute("Value", $"{docs.DocumentList[i].Beginning_funds_on_the_way}"), new XAttribute("Num", $"3")));
tagData.Add(new XElement("Px", new XAttribute("Value", $"{docs.DocumentList[i].End_balance}"), new XAttribute("Num", $"4")));
tagData.Add(new XElement("Px", new XAttribute("Value", $"{docs.DocumentList[i].End_funds_on_the_way}"), new XAttribute("Num", $"5")));
countofStroka++;
}
}
xDoc.Save(Path.Combine(Environment.CurrentDirectory, "xmldoc.xml"));
Ответы (1 шт):
Автор решения: Aziz Umarov
→ Ссылка
Добавляйте новый xml элемент в цикле. Вы в цикле добавляете одни и те же атрибуты в root элемент. Так и получается у вас дублирование атрибутов.