linq to xml C#

private string GetResponseErrorMsg(string transactionid, Dictionary<string, string> errorMsg)
{
//key除了head 都是 CTO 加msg
string xmlpath = HttpContext.Current.Request.PhysicalApplicationPath + "//XML//Receive//" + transactionid + ".xml";xml

XDocument doc = XDocument.Load(xmlpath);
XElement rootLinq = doc.Root;string


rootLinq.Name = "RESPONSE";
foreach (var item in errorMsg)
{
if (item.Key == "head")
{
XElement CTODhead = rootLinq.Element("HEADER");
if (CTODhead != null)
{it

XElement errorMsgRoot = new XElement("MESSAGE", item.Value);io

CTODhead.Add(errorMsgRoot);foreach

}
}select

XElement CTODelete = (from CTO in rootLinq.Element("BODY").Element("CTOS").Elements() where CTO.Element("NAME").Value == item.Key select CTO).SingleOrDefault();
if (CTODelete != null)
{
CTODelete.Element("CONFIGURATION").Remove();
XElement errorMsgRoot = new XElement("MESSAGE", item.Value);error

CTODelete.Add(errorMsgRoot);transaction

}let


}new

return doc.ToString(); }