title: c# adding xml element where attribute equals
body: <p>I have an xml file that has a bunch of channels, and I want to append a channel category to every single one of them. Depending on what channel it is. I'm very new to this so please excuse me if this is an obvious error.</p>
  
  <p>example: </p>
  
  <pre><code>&lt;channel-category&gt;Entertainment&lt;/channel-category&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;channel-category&gt;News&lt;/channel-category&gt;
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <pre><code>        string path;
          string xmlfile = "/channels.xml";
          path = Environment.CurrentDirectory + xmlfile;
  
          if (exists("channelname1"))
          {
              XmlDocument doc = new XmlDocument();
              doc.Load(path);
              XmlNode root = doc.DocumentElement;
              XmlNode node = root.SelectSingleNode("list/channel[@id='channelname1'");
              XmlNode category = doc.CreateElement("channel-category");
              category.InnerText = "channelcataegorygoeshere";
              node.AppendChild(category);
              doc.DocumentElement.AppendChild(node);
          }
          else
          {
              Console.WriteLine("not found");
          }
          Console.ReadKey();
      }
      static bool exists(string channelname)
      {
          string path;
          string xmlfile = "/channels.xml";
          path = Environment.CurrentDirectory + xmlfile;
          XDocument xmlDoc = XDocument.Load(path);
  
          bool doesexists = (from data in xmlDoc.Element("list").Elements("channel")
                         where (string)data.Attribute("id") == channelname
                         select data).Any();
          return doesexists;
      }
  </code></pre>
  
  <p>but it's giving me the following error and I can't figure it out.. What am I doing wrong?</p>
  
  <pre><code> An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll
      Additional information: 'list/channel[@id='channelname1'' has an invalid token.
  </code></pre>
  
  <p>from this line</p>
  
  <pre><code>XmlNode node = root.SelectSingleNode("list/channel[@id='channelname1'");
  </code></pre>
  
  <p>My XML looks like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;list info="list"&gt;
    &lt;channel id="channelname1"&gt;
      &lt;display-name lang="en"&gt;channelname1&lt;/display-name&gt;
      &lt;icon src="http://locationtologo.com/" /&gt;
      &lt;url&gt;http://someurl.com&lt;/url&gt;
    &lt;/channel&gt;
    &lt;channel id="channelname2"&gt;
      &lt;display-name lang="en"&gt;channelname2&lt;/display-name&gt;
      &lt;icon src="http://locationtologo.com/" /&gt;
      &lt;url&gt;http://someurl.com&lt;/url&gt;
    &lt;/channel&gt;
    &lt;channel id="channelname3"&gt;
      &lt;display-name lang="en"&gt;channelname3&lt;/display-name&gt;
      &lt;icon src="http://locationtologo.com/" /&gt;
      &lt;url&gt;http://someurl.com&lt;/url&gt;
    &lt;/channel&gt;
    &lt;channel id="channelname4"&gt;
      &lt;display-name lang="en"&gt;channelname4&lt;/display-name&gt;
      &lt;icon src="http://locationtologo.com/" /&gt;
      &lt;url&gt;http://someurl.com&lt;/url&gt;
    &lt;/channel&gt;
  &lt;/list&gt;
  </code></pre>