-1

我在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");
4

3 回答 3

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;
于 2013-01-07T13:14:59.423 回答
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>";

    }
}

编译器显示根据您的标准正确删除项目的输出,我们留下 . . .

<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" . . . />

于 2013-01-07T13:41:40.867 回答
0

从 XML 文件中删除节点在这里给出:http ://www.wrangle.in/topic/asw0zgftjzqr/C-Sharp-tricks-deleting-node-from-xml-fi

于 2013-01-07T13:43:17.347 回答