title: System.InvalidCastException: Failed to convert parameter value from a XElement to a String
body: <p><strong>Note:</strong> I was unable to find this exact question in a search. I found a somewhat similar question here on Stack Overflow, which led me to the solution. I'm posting the question and the solution so that the next person with the problem can more easily find the solution. I would have made the question CommunityWiki if that were still possible - I'm not looking for rep from this.</p>
  
  <hr>
  
  <p>I am trying to use ADO.NET to call a SQL Server 2005 stored procedure which accepts a parameter of the <code>Xml</code> type:</p>
  
  <pre><code>CREATE PROCEDURE dbo.SomeProcedure(
      @ListOfIds Xml)
  AS
  BEGIN
      DECLARE
              @Ids TABLE(ID Int);
      INSERT INTO @Ids
      SELECT ParamValues.ID.value('.', 'Int')
      FROM @ListOfIds.nodes('/Persons/id') AS ParamValues(ID);
  
      SELECT p.Id,
             p.FirstName,
             p.LastName
      FROM Persons AS p
           INNER JOIN @Ids AS i ON p.Id = i.ID;
  END;
  </code></pre>
  
  <p>I pass the XML as a LINQ to XML XElement object</p>
  
  <pre><code>var idList = new XElement(
      "Persons",
      from i in selectedPeople
      select new XElement("id", i));
  </code></pre>
  
  <p>later</p>
  
  <pre><code>SqlCommand cmd = new SqlCommand
                   {
                       Connection = conn,
                       CommandText = "dbo.SomeProcedure",
                       CommandType = CommandType.StoredProcedure
                   };
  cmd.Parameters.Add(
      new SqlParameter
      {
          ParameterName = "@ListOfIds",
          SqlDbType = SqlDbType.Xml,
          Value = idList)
      });
  using (var reader = cmd.ExecuteReader())
  {
      // process each row
  }
  </code></pre>
  
  <p>This fails on the <code>ExecuteReader</code> line with the exception:</p>
  
  <blockquote>
    <p>System.InvalidCastException: Failed to convert parameter value from a XElement to a String. ---> System.InvalidCastException: Object must implement IConvertible</p>
  </blockquote>
  
  <p>What's the correct way to pass an <code>XElement</code> to a stored procedure?</p>