从XML中删除节点

时间:2013-01-07 13:08:34

标签: c# xml linq-to-xml

我在XML

web.config以下
 <handlers>
  <remove name="ChartImageHandler" />            
  <add name="PageNotFoundhandelarrtf" path="*.rtf" verb="*" 
     modules="IsapiModule"  scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\
     aspnet_isapi.dll" resourceType="Unspecified"  preCondition=
     "classicMode,runtimeVersionv2.0,bitness32" />
  <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization,  Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="Keyoti_SearchEngine_Web_CallBackHandler_ashx" verb="*" preCondition="integratedMode" path="Keyoti.SearchEngine.Web.CallBackHandler.ashx" type="Keyoti.SearchEngine.Web.CallBackHandler, Keyoti2.SearchEngine.Web, Version=2012.5.12.706, Culture=neutral, PublicKeyToken=58d9fd2e9ec4dc0e" />
  <add path="Reserved.ReportViewerWebControl.axd" 
     verb="*"  type="Microsoft.Reporting.WebForms.HttpHandler,
     Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral,  PublicKeyToken
     =b03f5f7f11d50a3a" validate="false" />
 </handlers>

我需要从XML部分ReportViewer删除此<handler>的最后一个节点。我首先需要找到<handler>部分而不是上面的节点。

我正在使用下面的代码,但它不起作用...请你指导我下面的代码有什么问题..

XElement xEmp = XElement.Load(PATH + WEB_CONFIG_PATH);
var empDetails = from emps in xEmp.Elements("handlers")
                 where emps.Element("path").Equals("Reserved.ReportViewerWebControl.axd")
                 select emps;
empDetails.First().Remove();
xEmp.Save(@"D:\Employees.xml");

3 个答案:

答案 0 :(得分:2)

尝试使用下一个代码段

XElement xEmp = XElement.Load(PATH + WEB_CONFIG_PATH);
var pathToRemove = "Reserved.ReportViewerWebControl.axd";

var empDetails= xEmp.XPathSelectElements("//handlers")
                    .Descendants()
                    .First(d => d.Attributes().Any(atr => atr.Name == "path" && atr.Value == pathToRemove));

empDetails.Remove();
xEmp.Save(@"D:\Employees.xml");

如果你想坚持使用查询语法,你仍然需要稍微混合一下:

var empDetails = from emps in xEmp.XPathSelectElements("//handlers").Descendants()
                 where emps.Attributes().Any(atr => atr.Name == "path" && atr.Value == pathToRemove)
                 select emps;

答案 1 :(得分:0)

我运行此代码似乎有效。这就是我做的。 。

XDocument doc = XDocument.Parse(INPUT_DATA);
XElement handlers = doc.Element("handlers");
IEnumerable<XElement> add = null;
IEnumerable<XElement> pFind = null;

if (handlers != null)
{             
    add = handlers.Elements();
    if (add != null)
    {

        pFind = (from itm in add
                    where itm.Attribute("path") != null
                    && itm.Attribute("path").Value != null
                    && itm.Attribute("path").Value == "Reserved.ReportViewerWebControl.axd"
                    select itm);

        if(pFind != null)
            pFind.FirstOrDefault().Remove();
    }
}

这是完整的测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace XDocu
{
    class Program
    {
        static void Main(string[] args)
        {
        XDocument doc = XDocument.Parse(INPUT_DATA);
        XElement handlers = doc.Element("handlers");
        IEnumerable<XElement> add = null;
        IEnumerable<XElement> pFind = null;
        int oldCount = doc.Element("handlers").Elements().Count();

        if (handlers != null)
        {             
            add = handlers.Elements();
            if (add != null)
            {

                pFind = (from itm in add
                            where itm.Attribute("path") != null
                            && itm.Attribute("path").Value != null
                            && itm.Attribute("path").Value == "Reserved.ReportViewerWebControl.axd"
                            select itm);

                if(pFind != null)
                    pFind.LastOrDefault().Remove();
            }
        }

            //print it
            if (add != null)
                Console.WriteLine("Old Count: {0}\nNew Count: {1}", oldCount, add.Count());
        }

        const string INPUT_DATA =
        @"<?xml version=""1.0""?>
        <handlers>
        <remove name=""ChartImageHandler"" />            
        <add name=""PageNotFoundhandelarrtf"" path=""*.rtf"" verb=""*"" 
            modules=""IsapiModule""  scriptProcessor=""%windir%\Microsoft.NET\Framework\v2.0.50727\
            aspnet_isapi.dll"" resourceType=""Unspecified""  preCondition=
            ""classicMode,runtimeVersionv2.0,bitness32"" />
        <add name=""ChartImageHandler"" preCondition=""integratedMode"" verb=""GET,HEAD"" path=""ChartImg.axd"" type=""System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization,  Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" />
        <add name=""Keyoti_SearchEngine_Web_CallBackHandler_ashx"" verb=""*"" preCondition=""integratedMode"" path=""Keyoti.SearchEngine.Web.CallBackHandler.ashx"" type=""Keyoti.SearchEngine.Web.CallBackHandler, Keyoti2.SearchEngine.Web, Version=2012.5.12.706, Culture=neutral, PublicKeyToken=58d9fd2e9ec4dc0e"" />
        <add path=""Reserved.ReportViewerWebControl.axd"" 
            verb=""*""  type=""Microsoft.Reporting.WebForms.HttpHandler,
            Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral,  PublicKeyToken
            =b03f5f7f11d50a3a"" validate=""false"" />
        </handlers>";

    }
}

编译器显示输出,正确地根据您的critera删除项目,我们留下。 。

<handlers>
  <remove name="ChartImageHandler" />
  <add name="PageNotFoundhandelarrtf" path="*.rtf" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\             aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
  <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization,  Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="Keyoti_SearchEngine_Web_CallBackHandler_ashx" verb="*" preCondition="integratedMode" path="Keyoti.SearchEngine.Web.CallBackHandler.ashx" type="Keyoti.SearchEngine.Web.CallBackHandler, Keyoti2.SearchEngine.Web, Version=2012.5.12.706, Culture=neutral, PublicKeyToken=58d9fd2e9ec4dc0e" />
</handlers>

即排除<Add path="Reserved.ReportViewerWebControl.axd" . . . />

答案 2 :(得分:0)