diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index a1085ab..da59c6f 100644 --- a/src/MiniWord/MiniWord.Implment.cs +++ b/src/MiniWord/MiniWord.Implment.cs @@ -202,6 +202,8 @@ private static object GetObjVal(object objSource, string propNames) /// private static object GetObjVal(object objSource, string[] propNames) { + if(objSource == null) return null; + var nextPropNames = propNames.Skip(1).ToArray(); if (objSource is IDictionary) { @@ -777,15 +779,17 @@ private static void ReplaceIfStatements(OpenXmlElement rootXmlElement, Listc == elementList[i])) rootXmlElement.RemoveChild(elementList[i]); + elementList[i].Remove(); } } - if(rootXmlElement.ChildElements.Any(c => c == ifP)) - rootXmlElement.RemoveChild(ifP); - if (rootXmlElement.ChildElements.Any(c => c == endIfP)) - rootXmlElement.RemoveChild(endIfP); + // 从paragraphs中移除,防止死循环 paragraphs.Remove(ifP); paragraphs.Remove(endIfP); + // 从doc元素移除 + if (ifP.Parent != null) + ifP.Remove(); + if (endIfP.Parent != null) + endIfP.Remove(); } }