1

I need to be able to add XML data to an already created parent node and place it under specific parent nodes.

Does anyone have any suggestions on the best way to do this?

I used the XMLWriter to create the original XML file.

 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");
                            }
                        }
4
  • Please specify what actually is not working. You have checkDog but you don't use it. Is checkEvent really != null? Commented Aug 7, 2012 at 19:04
  • just the code above is not working. ignore the rest. and yes I am loading the XML file first Commented Aug 7, 2012 at 19:09
  • But now you've edited out what is checkEvent, making the question more ambiguous. When you debug, do you see the new element appear under checkEvent? Commented Aug 7, 2012 at 19:12
  • sorry I repasted in the entire code aove. I am not seeing the new element that is created under if(checkDog==null) that is the entire issue here. I have checked and I have gotten basic stuff like editing the text value of a text box to work under the if(checkDog==null) but I have not gotten the code shown above to work to add the element Commented Aug 7, 2012 at 19:23

1 Answer 1

1

EDIT: As code changed my old answer became invalid. Anyway I tested Your code in Visual Studio. It worked. Here is my input xml file:

<Event>
  <Event id="0">

  </Event>
</Event>

and output:

<Event>
  <Event id="0">
    <Dog id="1">
      <dogName id="2" />
      <breed id="3" />
    </Dog>
  </Event>
</Event>

My guess is that You have something wrong in Your XML file and code in ifs isn't executed. I think XmlSerializer is better, and less error prone method for simple XML creating/updating.

Sign up to request clarification or add additional context in comments.

5 Comments

yes the file is loaded I do have the code you have above to load the xml file. I copied and pasted the code from above wrong the if statement that is being excuted has ==null instead so it is executed. The problem is that the code avobe is not working to edit the XML file.
Interesting? You were able to add it to an existing XML file. could it possibly be a problem if my XML file is hidden?
It should not matter. If doc.Load() didn't throw Exception, than file is loaded. Have You confirmed that code, up to Save() method is executed? If it executes Save() without exeption it should update file. Could You add Your sample input XML?
just verified it is the fact that I am hiding this xml file. Any suggestjions on the most efficient way for hiding, saving, then unhiding this file. I have an idea, but am always open for learning more efficient ways
I've just checked it too. You're right, but when file is hidden, Save() method throws an exception. Anyway - use File.SetAttributes(filePath, FileAttributes.Normal); and File.SetAttributes(filePath, FileAttributes.Hidden); for hiding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.