Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#根据内部文本删除XML节点,而不知道父节点_C#_Xml - Fatal编程技术网

C#根据内部文本删除XML节点,而不知道父节点

C#根据内部文本删除XML节点,而不知道父节点,c#,xml,C#,Xml,我有一个结构如下的文档,我正在尝试删除所有出现的“delete ME”。这背后没有XPath模式。最好的方法是什么 下面是原始XML <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Data> <PageInfo> <ID>0</ID> <NUM>DELETE ME</NUM> <URL>er.php</UR

我有一个结构如下的文档,我正在尝试删除所有出现的“delete ME”。这背后没有XPath模式。最好的方法是什么

下面是原始XML

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Data>
  <PageInfo>
   <ID>0</ID>
   <NUM>DELETE ME</NUM>
   <URL>er.php</URL>
  </PageInfo>
  <SomeOtherAttributeName>
   <ID>1</ID>
   <NUM> 
       <INDX>DELETE ME</INDX>
       <somethingElse>keep me here</somethingElse>
   </NUM>
   <URL>/out/out.ViewFolder.php</URL>
  </SomeOtherAttributeName>
</Data>

0
删除我
er.php
1.
删除我
让我留在这里
/out/out.ViewFolder.php
期望输出为

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Data>
  <PageInfo>
   <ID>0</ID>
   <URL>er.php</URL>
  </PageInfo>
  <SomeOtherAttributeName>
   <ID>1</ID>
   <NUM> 
       <somethingElse>keep me here</somethingElse>
   </NUM>
   <URL>/out/out.ViewFolder.php</URL>
  </SomeOtherAttributeName>
</Data>

0
er.php
1.
让我留在这里
/out/out.ViewFolder.php

使用Xml Linq。请参阅下面的代码

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

namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> deleteMe = doc.Descendants().Where(x => (string)x == "DELETE ME").ToList();

            for (int i = deleteMe.Count - 1; i >= 0; i--)
            {
                deleteMe[i].Remove();
            }
        }

    }



}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序75
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List deleteMe=doc.substands()。其中(x=>(字符串)x==“DELETE ME”).ToList();
对于(int i=deleteMe.Count-1;i>=0;i--)
{
deleteMe[i].Remove();
}
}
}
}

使用Xml Linq。请参阅下面的代码

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

namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> deleteMe = doc.Descendants().Where(x => (string)x == "DELETE ME").ToList();

            for (int i = deleteMe.Count - 1; i >= 0; i--)
            {
                deleteMe[i].Remove();
            }
        }

    }



}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序75
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List deleteMe=doc.substands()。其中(x=>(字符串)x==“DELETE ME”).ToList();
对于(int i=deleteMe.Count-1;i>=0;i--)
{
deleteMe[i].Remove();
}
}
}
}

你所说的“这背后没有XPath模式”是什么意思?我的意思是我不想搜索“Data/PageInfo/Num”来获得“DELETE ME”。我想找到所有内部文本为“DELETE ME”的节点,然后从原始XML中删除这些节点。文件/字符串有多大?你说的“这背后没有XPath模式”是什么意思?我的意思是我不想搜索“Data/PageInfo/Num”来获得“DELETE ME”。我想找到所有内部文本为“DELETE ME”的节点,然后从原始XML中删除这些节点。文件/字符串有多大?您缺少doc.Save(“c:\temp\test.new.XML”):)您缺少doc.Save(“c:\temp\test.new.XML”):)