I have an XDocument and i want to remove XElement from this.
Please see the following code , Why not remove the XElement ?
xml file :
<Reports>
<Report>
<Id>45f6bf21-d1b4-431b-818c-c1cb1c9bb221</Id>
<Content>Example 1</Content>
<Date>2014/10/11</Date>
<Time>18:03</Time>
</Report>
<Report>
<Id>15c74518-64c0-459d-98a3-831734d96a76</Id>
<Content>Example 2</Content>
<Date>1393/10/12</Date>
<Time>04:00</Time>
</Report>
<Report>
<Id>a2a48e10-4b16-4484-8402-c13a74af3981</Id>
<Content>Example 3</Content>
<Date>2014/10/13</Date>
<Time>03:36</Time>
</Report>
</Reports>
service :
public class ReportService : IReportService
{
private readonly List<Report> allReports;
private readonly XDocument data;
public ReportService()
{
allReports = new List<Report>();
data = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Reports.xml"));
var reports = from st in data.Descendants("Report")
select new Report
{
Id = st.Element("Id").Value,
Content = st.Element("Content").Value,
Date = st.Element("Date").Value,
Time = st.Element("Time").Value
};
allReports.AddRange(reports.ToList());
}
public void Delete(string id)
{
Delete(d => d.Element("Id").ToString() == id);
}
public void Delete(Func<XElement, bool> @where)
{
data.Root.Elements("Report").Where(@where).Remove();
data.Save(HttpContext.Current.Server.MapPath("~/App_Data/Reports.xml"));
}
}
public interface IReportService
{
void Delete(string id);
void Delete(Func<XElement, bool> @where);
}
my code :
public class ReportsController : Controller
{
private readonly IReportService _reportService;
public ReportsController()
{
_reportService = new ReportService();
}
public void Delete(string id)
{
_reportService.Delete(id);
}
}
no errors. But does not remove the XElement . How do I fix it?
I need to remove an XElement from a XDocument.
No comments:
Post a Comment