C# 替换xml元素

C# 替换xml元素,c#,.net,linq-to-xml,C#,.net,Linq To Xml,我在做这样的事情: var results = from d in myDocument.Elements().Descendants("myName") select d; foreach (var result in results){ if (sth...){ result.replace(myXElement); } } 问题是,当我替换一个元素时,我不能迭代到下一个元素,因为存在空引用。var导致结果 是否有其他方法可以用另一个元素替换该元素,并且仍然能够遍

我在做这样的事情:

var results = from d in myDocument.Elements().Descendants("myName") select d;

foreach (var result in results){
   if (sth...){
      result.replace(myXElement);
   }
}
问题是,当我替换一个元素时,我不能迭代到下一个元素,因为存在空引用。var导致结果

是否有其他方法可以用另一个元素替换该元素,并且仍然能够遍历该文档

谢谢你的帮助

堆栈跟踪是:

System.NullReferenceException was unhandled Message="Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu." Source="System.Xml.Linq" StackTrace: w System.Xml.Linq.XContainer.d__a.MoveNext() w System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() w DocSorter.Merge.MergeFiles(String contentFilePath, String directoryPath) w D:\DocSorter\DocSorter\Merge.cs:wiersz 39 w DocSorter.MainBox.btnMergeFiles_Click(Object sender, EventArgs e) w D:\DocSorter\DocSorter\MainBox.cs:wiersz 85 w System.Windows.Forms.Control.OnClick(EventArgs e) w System.Windows.Forms.Button.OnClick(EventArgs e) w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) w System.Windows.Forms.Control.WndProc(Message& m) w System.Windows.Forms.ButtonBase.WndProc(Message& m) w System.Windows.Forms.Button.WndProc(Message& m) w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) w System.Windows.Forms.Application.Run(Form mainForm) w DocSorter.Program.Main() w D:\DocSorter\DocSorter\Program.cs:wiersz 18 w System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() w System.Threading.ThreadHelper.ThreadStart_Context(Object state) w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) w System.Threading.ThreadHelper.ThreadStart() InnerException:
在IEnumerable中循环时不能修改它

由于linqtoxml使用延迟执行,因此它试图在修改XML时找到子体

要解决此问题,您需要在循环元素之前将元素放入数组中,如下所示:

var results = myDocument.Descendants("myName").ToArray();

foreach (var result in results){
   if (sth...){
      result.replace(myXElement);
   }
}

通过调用ToArray,您可以强制它立即枚举元素,而不是在循环元素时查找它们。

看来您已经使用了自己的扩展方法replacemyXElement。但在这种情况下,这似乎能满足你的需要

以下是Microsoft从其文档中获取的ReplaceWithObject示例:

==代码==

XElement xmlTree=新XElementRoot, 新XElementChild1,child1内容, 新XElementChild2,child2内容, 新XElementChild3,child3内容, 新XElementChild4、child4内容、, 新XElementChild5、child5内容 ; XElement child3=xmlTree.ElementChild3; 孩子3.替换为 新XElementNewChild,新内容 ; Console.WriteLinexmlTree; ==预期输出==

child1内容 儿童2内容 新内容 儿童4内容 儿童5内容
var results=myDocument.genderantsMyName;异常的调用堆栈是什么?我编辑了我的问题,用调用堆栈填充了它。谢谢,还有一个方法叫做ReplaceNodes,它可能对其他人有用。