<p>I have a simple CAML query like</p>

<pre><code>&lt;Where&gt;&lt;Eq&gt;&lt;Field="FieldName"&gt;&lt;Value Type="Text"&gt;Value text&lt;/Value&gt;&lt;/Field&gt;&lt;/Eq&gt;&lt;/Where&gt;
</code></pre>

<p>And I have a variable to substitute for <code>Value text</code>. What's the best way to validate/escape the text that is substituted here in the .NET framework?
I've done a quick web search on this problem but all what I found was <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.aspx" rel="nofollow noreferrer"><code>System.Xml.Convert</code></a> class but this seems to be not quite what I need here.</p>

<p>I know I could have gone with an <code>XmlWriter</code> here, but it seems like a lot of code for such a simple task where I just need to make sure that the <code>Value text</code> part is formatted well.</p>

[{'answer_id': 17093, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>use <code>System.Xml.Linq.XElement</code> and <code>SetValue</code> method. This will format the text <em>(assuming a string)</em>, but also allows you to set xml as the value.</p>\n'}, {'answer_id': 17137, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>I am not sure what context the xml is coming from, but if it is stored in a string const variable that you created, then the easiest way to modify it would be:</p>\n\n<pre><code>public class Example\n{\n    private const string CAMLQUERY = "&lt;Where&gt;&lt;Eq&gt;&lt;Field=\\"FieldName\\"&gt;&lt;Value Type=\\"Text\\"&gt;{0}&lt;/Value&gt;&lt;/Field&gt;&lt;/Eq&gt;&lt;/Where&gt;";\n\n    public string PrepareCamlQuery(string textValue)\n    {\n        return String.Format(CAMLQUERY, textValue);\n    }\n}\n</code></pre>\n\n<p>Of course, this is the easiest approach based on the question.  You could also store the xml in an xml file and read it in and manipulate it that way, like what <a href="https://stackoverflow.com/questions/17085/escaping-xml-tag-contents#17093">Darren Kopp</a> answered.  That also requires C# 3.0 and I am not sure what .Net Framework you are targeting.  If you aren\'t targeting .Net 3.5 and you want to manipulate the Xml, I recommend just using Xpath with C#.  This <a href="http://www.codeproject.com/KB/cpp/myXPath.aspx" rel="nofollow noreferrer">reference</a> goes into detail on using xpath with C# to manipulate xml, than me typing it all out.</p>\n'}, {'answer_id': 22293, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 0, 'selected': False, 'text': '<p>You can use the System.XML namespace to do it.  Of course you can also use LINQ.  But I choose the .NET 2.0 approach because I am not sure which version of .NET you are using.</p>\n\n<pre><code>XmlDocument doc = new XmlDocument();\n\n// Create the Where Node\nXmlNode whereNode = doc.CreateNode(XmlNodeType.Element, "Where", string.Empty);\nXmlNode eqNode = doc.CreateNode(XmlNodeType.Element, "Eq", string.Empty);\nXmlNode fieldNode = doc.CreateNode(XmlNodeType.Element, "Field", string.Empty);\n\nXmlAttribute newAttribute = doc.CreateAttribute("FieldName");\nnewAttribute.InnerText = "Name";\nfieldNode.Attributes.Append(newAttribute);\n\nXmlNode valueNode = doc.CreateNode(XmlNodeType.Element, "Value", string.Empty);\n\nXmlAttribute valueAtt = doc.CreateAttribute("Type");\nvalueAtt.InnerText = "Text";\nvalueNode.Attributes.Append(valueAtt);\n\n// Can set the text of the Node to anything.\nvalueNode.InnerText = "Value Text";\n\n// Or you can use\n//valueNode.InnerXml = "&lt;aValid&gt;SomeStuff&lt;/aValid&gt;";\n\n// Create the document\nfieldNode.AppendChild(valueNode);\neqNode.AppendChild(fieldNode);\nwhereNode.AppendChild(eqNode);\n\ndoc.AppendChild(whereNode);\n\n// Or you can use XQuery to Find the node and then change it\n\n// Find the Where Node\nXmlNode foundWhereNode = doc.SelectSingleNode("Where/Eq/Field/Value");\n\nif (foundWhereNode != null)\n{\n    // Now you can set the Value\n    foundWhereNode.InnerText = "Some Value Text";\n}\n</code></pre>\n'}, {'answer_id': 22297, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 1, 'selected': False, 'text': "<p>When working with XML, always use the XML API that works with your programming environment.  Don't try to roll your own XML document building and escaping code.  As Longhorn213 mentioned, in .Net all the appropriate stuff is in the System.XML namespace.  Trying to to write your own code for writing XML documents will just result in many bugs and troubles down the line.</p>\n"}, {'answer_id': 26202, 'author': 'axk', 'author_id': 578, 'author_profile': 'https://Stackoverflow.com/users/578', 'pm_score': 1, 'selected': False, 'text': '<p>The problem with the System.Xml approach in my case was that it required too much code to build this simple XML fragment. I think I\'ve found a compromise.</p>\n\n<pre><code>XmlDocument doc = new XmlDocument();\ndoc.InnerXml = @"&lt;Where&gt;&lt;Eq&gt;&lt;Field Name=""FieldName""&gt;&lt;Value Type=""Text""&gt;/Value&gt;&lt;/Field&gt;&lt;/Eq&gt;&lt;/Where&gt;";\nXmlNode valueNode = doc.SelectSingleNode("Where/Eq/Field/Value");\nvalueNode.InnerText = @"Text &lt;&gt;!$% value&gt;";\n</code></pre>\n'}, {'answer_id': 1466268, 'author': 'jedigo', 'author_id': 94345, 'author_profile': 'https://Stackoverflow.com/users/94345', 'pm_score': 1, 'selected': False, 'text': '<p>Use this:</p>\n\n<pre><code>System.Security.SecurityElement.Escape("&lt;unescaped text&gt;");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/17085', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/578/']