
Wenn ich versuche den Knoten zu löschen kommt der Fehler : The Node to be removed is not a Child of this node.
Ich erkenne aber den Fehler leider nicht.
C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace codeWatcherConsoleVersion01
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
List<string> allPupils = File.ReadAllLines("PupilNames.csv").ToList();
string placeholder = "repeating";
XmlNodeList allElem = getElement(doc, "ul"); //Ich kriege hier alle uls heraus Falls es mehrere uls an verschiedenen Stellen gibt, die nicht wiederholt werden müssen setze...
XmlNode repeatData; //ich mir ein "Node" welches später das richtige ul besitzt (welches wiederholt wird)....
XmlElement templateData = doc.DocumentElement;
repeatData = findRepeatElement(allElem,placeholder,"pupil");
templateData.RemoveChild(repeatData); //HIER TRITT DER BUG AUF
}
#region Private Methods
private static XmlNode findRepeatElement(XmlNodeList allElem,string searchAttribute,string searchAttrValue)
{
XmlNode repeatNode = null;
int listenIdx = 0;
while (listenIdx < allElem.Count && repeatNode == null)
{
if (checkAttribute(allElem[listenIdx], searchAttribute))
{
if (allElem[listenIdx].Attributes[searchAttribute].Value == searchAttrValue)
{
repeatNode = allElem[listenIdx];
}
}
listenIdx++;
}
if (repeatNode == null)
throw new Exception("Es wurde kein Element gefunden, welches widerholt werden muss!");
return repeatNode;
}
private static bool checkAttribute(XmlNode element, string attributeName)
{
if (element.Attributes[attributeName] == null)
return false;
else
return true;
}
private static XmlNodeList getElement(XmlDocument doc, string xmlElementName)
{
if(doc.GetElementsByTagName(xmlElementName).Count == 0)
{
throw new Exception("Element not found!");
}
return doc.GetElementsByTagName(xmlElementName);
}
private static bool checkIfSameAttrValue(XmlNodeList allElem,int listIdx, string searchAttribute,string searchAttrValue)
{
if (allElem[listIdx].Attributes[searchAttribute].Value != searchAttrValue)
return false;
else
return true;
}
#endregion
}
}