Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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# 使用Linq删除XML文档的部分_C#_Linq To Xml - Fatal编程技术网

C# 使用Linq删除XML文档的部分

C# 使用Linq删除XML文档的部分,c#,linq-to-xml,C#,Linq To Xml,如何使用Linq删除其元素包含参数为{}的所有部分?在我的示例中,我想删除带有{SecName1}的节 源文件: <ReceiptLayoutMaintenanceRequest> <ReceiptLayoutName>Test Layout1</ReceiptLayoutName> <ActionName>Add</ActionName> <ReceiptLayoutForMaintenance> <

如何使用Linq删除其元素包含参数为{}的所有部分?在我的示例中,我想删除带有{SecName1}的节

源文件:

<ReceiptLayoutMaintenanceRequest>
  <ReceiptLayoutName>Test Layout1</ReceiptLayoutName> 
  <ActionName>Add</ActionName> 
 <ReceiptLayoutForMaintenance>
  <Name>Test Layout1</Name> 
  <Description>ReciptDesc</Description> 
  <PrinterName>Emulator - Receipt</PrinterName> 
  <ReceiptLayout>
    <Name>AAA</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
  <ReceiptLayout>
    <Name>{SecName1}</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
 </ReceiptLayoutForMaintenance>
</ReceiptLayoutMaintenanceRequest>

测试布局1
添加
测试布局1
往复式ESC
仿真器-收据
AAA
$ 
默认模板
{SecName1}
$ 
默认模板
想要的产出

<ReceiptLayoutMaintenanceRequest>
  <ReceiptLayoutName>Test Layout1</ReceiptLayoutName> 
  <ActionName>Add</ActionName> 
 <ReceiptLayoutForMaintenance>
  <Name>AAA</Name> 
  <Description>ReciptDesc</Description> 
  <PrinterName>Emulator - Receipt</PrinterName> 
  <ReceiptLayout>
    <Name>AAA</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
 </ReceiptLayoutForMaintenance>

测试布局1
添加
AAA
往复式ESC
仿真器-收据
AAA
$ 
默认模板


感谢

这将删除任何具有子
名称
ReceiptLayout
节点,该子节点以括号开头和结尾,并生成所需的输出:

XDocument doc = XDocument.Load(@"test.xml"); //load xml
var nodesToRemove = doc.Descendants("ReceiptLayout")
                       .Where(x => x.Element("Name").Value.StartsWith("{") 
                                && x.Element("Name").Value.EndsWith("}"))
                       .ToList();

foreach (var node in nodesToRemove)
    node.Remove();
这可以缩短为一个Linq语句,但我个人更喜欢将Linq查询和修改(删除)分开:

doc.Descendants("ReceiptLayout")
   .Where(x => x.Element("Name").Value.StartsWith("{") 
            && x.Element("Name").Value.EndsWith("}"))
   .Remove();

这将删除任何
ReceiptLayout
节点,该节点具有以括号开头和结尾的子
名称
,并生成所需的输出:

XDocument doc = XDocument.Load(@"test.xml"); //load xml
var nodesToRemove = doc.Descendants("ReceiptLayout")
                       .Where(x => x.Element("Name").Value.StartsWith("{") 
                                && x.Element("Name").Value.EndsWith("}"))
                       .ToList();

foreach (var node in nodesToRemove)
    node.Remove();
这可以缩短为一个Linq语句,但我个人更喜欢将Linq查询和修改(删除)分开:

doc.Descendants("ReceiptLayout")
   .Where(x => x.Element("Name").Value.StartsWith("{") 
            && x.Element("Name").Value.EndsWith("}"))
   .Remove();

使用
let
关键字的BrokenGlass代码的变体

var doc = XDocument.Load(@"test.xml");

var list = from p in doc.Descendants("ReceiptLayout")
           let q = p.Element("Name")
           let r = q != null ? q.Value : string.Empty
           where r.StartsWith("{") && r.EndsWith("}")
           select p;

list.Remove();

这是“过早优化”:-I“缓存”p.Element(“Name”).Value。啊。。。我检查是否真的有
名称
元素,这样如果没有一个元素,一切都不会崩溃:-)

一种使用
let
关键字的BrokenGlass代码变体

var doc = XDocument.Load(@"test.xml");

var list = from p in doc.Descendants("ReceiptLayout")
           let q = p.Element("Name")
           let r = q != null ? q.Value : string.Empty
           where r.StartsWith("{") && r.EndsWith("}")
           select p;

list.Remove();

这是“过早优化”:-I“缓存”p.Element(“Name”).Value。啊。。。我检查是否真的有
名称
元素,这样如果没有一个元素,一切都不会崩溃:-)

第一个代码示例在foreach中为我提供
NullReferenceException
。也许他在创作时你不喜欢碰他的收藏。第二个有效。在第一个查询中添加一个ToList应该可以解决这个问题。@xanatos:很好,我不知道我是如何忽略这个问题的-在第一个示例中添加了一个
ToList()
,这解决了第一个代码示例在foreach中给我的
NullReferenceException
问题。也许他在创作时你不喜欢碰他的收藏。第二个有效。在第一个查询中添加一个ToList应该可以解决它。@xanatos:很好,我不知道我是如何忽略了这一点-在第一个示例中添加了一个
ToList()
,这解决了问题抱歉,但两个答案没有帮助。我仍然无法删除此分区(@user681055正则表达式中存在故障。请立即尝试。抱歉,2个答案没有帮助。我仍然无法删除此分区(@user681055正则表达式中存在故障。请立即尝试。