I have the following XML file:

<TabelTag val = "99">

I want to write code to read this file and add it if the specified tag does not exist.

XElement xml = XElement.Load (@ "C: \ Filename.xml");
XElement tblInfo = (from item in xml.Elements ("TableTag")
                   where (string) item.Attribute ("val") == "666"
                   select item) .FirstOrDefault ();
if (null == tblInfo)
{// Add table
    // set new table info
    tblInfo = new XElement ("TableTag",
                  new XAttribute ("value", "666")
    // add information
    xml.Add (tblInfo);
xml.Save (@ "C: \ sample.xml");

I expected to output "sample.xml" to which the tag "TableTag val =" 666 "" was added with this code, but "if (null == tblInfo)" does not seem to work.
How can I determine if the tag does not exist?

  • Answer # 1

    <TableTag val = "99">
      <TableTag val = "666" />

    I have confirmed that.
    I seem to have overlooked something.