newToJava
newToJava

Reputation: 11

How do I check if an XML element exists in C# before adding a new element?

I've seen the many postings on this issue, but can't seem to get my C# code to work.

Basically, I have an XML document that contains elements called "color", which I would like my function addColor to add a new element and value to the xml if it doesn't already exist. I know that hard coding is bad practice and I plan to change that, I just need to resolve this issue first.

My question is, why isn't my code looping through each "color" element and breaking once there is a match between the current element value and the input value to the function?

Below is my current XML File:

<?xml version="1.0"?>
 <configuration>
   <color>Black</color>
   <color>Brown</color>
   <color>Gray</color>
  <color>Orange</color>
  <color>Pink</color>
  <color>Purple</color>
  <color>Red</color>
  <color>White</color>
  <color>Yellow</color>
</configuration>

My code looks like this:

        public void addColor(string newColor)
    {
        XmlDocument document = new XmlDocument();
        //adds child element to Make element
        try
        {

            document.Load("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");

            XmlNode root = document.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("configuration");
            foreach (XmlNode node in nodes)
            {
                if (node.Value == newColor)
                {
                    break;
                }
            }


                XmlElement childElement = document.CreateElement("color");
            childElement.InnerText = newColor;
            XmlNode parentNode = document.SelectSingleNode("configuration");
            parentNode.InsertAfter(childElement, parentNode.LastChild);
            document.Save("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");


        }//try
        catch (XmlException e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine("Exception object Line, pos: (" + e.LineNumber + "," + e.LinePosition + ")");
        }//end catch
    }//end addMake function

Upvotes: 0

Views: 5342

Answers (1)

Ňuf
Ňuf

Reputation: 6217

This will fix it. As there is more than one problem, I put explanations directly in code as comments.

public void addColor(string newColor)
{
    XmlDocument document = new XmlDocument();
    //adds child element to Make element
    try
    {
        document.Load("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");

        XmlNode root = document.DocumentElement;

        // "configuration" node is what you already have in "root" variable.
        // You want to select "color" nodes that are children of this root element
        XmlNodeList nodes = root.SelectNodes("color");
        //XmlNodeList nodes = root.SelectNodes("configuration");

        foreach (XmlNode node in nodes)
        {
            // node.Value returns null for node elements - see
            // https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.value?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(System.Xml.XmlNode.Value);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.7);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7
            // You must use node.InnerText instead
            if (node.InnerText == newColor)
            //if (node.Value == newColor)
            {
                // BREAK would only exit from foreach loop, but method
                // would continue execution immediately after foreach
                // i.e. with document.CreateElement("color");
                // and new color node would be added regardless of
                // whether it already exists or not.
                // RETURN exits method completely
                return;
                //break;
            }
        }

        XmlElement childElement = document.CreateElement("color");
        childElement.InnerText = newColor;
        XmlNode parentNode = document.SelectSingleNode("configuration");
        parentNode.InsertAfter(childElement, parentNode.LastChild);
        document.Save("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");


    }//try
    catch (XmlException e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine("Exception object Line, pos: (" + e.LineNumber + "," + e.LinePosition + ")");
    }//end catch
}//end addMake function


You can also use LINQ to XML instead, which usually results in shorter and more readable code:

public static void addColor2(string newColor)
{
    var document = XDocument.Load("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");
    if(!document.Root.Elements("color").Any(element => element.Value == newColor))
    {
        document.Root.Add(new XElement("color", newColor));
        document.Save("C:\\Users\\tobiajj1\\Desktop\\XML_C#\\testDetailManager\\original.xml");
    }
}

Upvotes: 1

Related Questions