如何在PowerShell中编写Xml.linq?

如何在PowerShell中编写Xml.linq?,xml,powershell,linq-to-xml,Xml,Powershell,Linq To Xml,我正在PowerShell中尝试这样做: XDocument document=XDocument.Load(@“web.config”); var comments=document.subjects(“客户端”).subjectantnodes().OfType().ToArray(); foreach(注释中的var注释) { XElement unCommented=XElement.Parse(comment.Value); 注释.替换为(未注释); } 我试过这样的方法: $xDo

我正在PowerShell中尝试这样做:

XDocument document=XDocument.Load(@“web.config”);
var comments=document.subjects(“客户端”).subjectantnodes().OfType().ToArray();
foreach(注释中的var注释)
{
XElement unCommented=XElement.Parse(comment.Value);
注释.替换为(未注释);
}
我试过这样的方法:

$xDoc = [System.Xml.Linq.XDocument]::Load("web.config")

[System.Collections.Generic.IEnumerable[System.Xml.Linq.XElement]] $enum = $xDoc.Descendants("client")
       
$clients = [System.Xml.Linq.Extensions]::DescendantNodes($enum)
但是我有一个错误,说

使用1个参数调用后代节点时出现异常:值不能为null

我在powershell中使用linq实现了这一点(从xml文档中取消注释):

[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null

$xDoc = [System.Xml.Linq.XDocument]::Load("web.config")
$endpoints = $xDoc.Descendants("client") | foreach { $_.DescendantNodes()}               
$comments = $endpoints | Where-Object { $_.NodeType -eq [System.Xml.XmlNodeType]::Comment -and $_.Value -match "net.tcp://localhost:9876/RaceDayService" }        
$comments | foreach { $_.ReplaceWith([System.Xml.Linq.XElement]::Parse($_.Value)) }

$xDoc.Save("web.config")

如果要编写PowerShell,则需要创建一个文件,该文件将在
导入模块MyModule
调用时加载类似的依赖项:

# Comma-separated assemblies that must be loaded prior to importing this module
RequiredAssemblies = @("System.Xml.Linq")
这是为编写模块的人推荐的方法