I have a SharePoint 2010 solution with a custom web part. I am using C# in Visual Studio 2012. The user has selected a view from a list, which I am storing as an SPView object, mobjView. I want to replace the < OrderBy > portion with new code, but preserve the < Where > portion, so my question involves the best way to parse the CAML string. This is what I have, I couldn't get Regex to cooperate, so I am wondering if there is a simpler way to parse the string (Where clause shortened to '...' for clarity).
// old string "<OrderBy><FieldRef Name='Title' /></OrderBy><Where>...</Where>"
string strQuery = mobjView.Query.Substring(mobjView.Query.IndexOf("<Where>"))
strQuery = "<OrderBy><FieldRef Name='MyField' /></OrderBy>" + strQuery;
// new string "<OrderBy><FieldRef Name='MyField' /></OrderBy><Where>...</Where>"