我需要能够将 XML 数据添加到已创建的父节点并将其放置在特定的父节点下。
有没有人对最好的方法有任何建议?
我使用XMLWriter
来创建原始 XML 文件。
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
for (int counter=0; counter<registeredEventCount; counter++)
{
try
{
XmlNode checkEvent = doc.SelectSingleNode("Event/Event[@id='" + registeredArrayList[counter] + "']");
if (checkEvent != null)
{
try
{
XmlNode checkDog = doc.SelectSingleNode("Event/Event[@id='" + registeredArrayList[counter] + "']/Dog[@id='" + ukcNumberArrayList[counter] + "']");
if (checkDog == null)
{
XmlElement newDogId = doc.CreateElement("Dog");
newDogId.SetAttribute("id", ukcNumberArrayList[counter].ToString());
XmlElement newDogName = doc.CreateElement("dogName");
newDogName.SetAttribute("id", dogNameArrayList[counter].ToString());
XmlElement newDogBreed = doc.CreateElement("breed");
newDogBreed.SetAttribute("id", breedArrayList[counter].ToString());
checkEvent.AppendChild(newDogId);
newDogId.AppendChild(newDogName);
newDogId.AppendChild(newDogBreed);
doc.Save(filePath);
}
else
{
MessageBox.Show("You have already registered this dog for this specific event");
}
}