我正在尝试解析一个冗长的文件并删除我不想要的部分。从研究看来,OpenXml SDK 是操作和搜索单词 doc 的最简单参考。不幸的是,它并不总是一致的,因为我NullReferenceExceptions
在尝试将节点分配给运行对象时不断得到。本质上,我的程序应该遍历 docx 文件并找到标签 (ver 1),然后删除它和结束标签 (/ver 1) 之间的所有内容。这似乎只适用于某些部分,因为我得到了其他部分NullReferenceException
,我觉得这与 MS Word 使用的凌乱格式有关,但我不知道。
如果有人可以帮助我欣赏它,这是特定部分的代码。
IEnumerable<OpenXmlElement> elem = main.Document.Body.Descendants().ToList();
foreach (OpenXmlElement elems in elem)
{
if (elems is Text && elems.InnerText == s_Ver1)// s_Ver1 = "(Ver 1)"
{
Run run = (Run)elems.Parent;
Paragraph p = (Paragraph)run.Parent;
p.RemoveAllChildren();
p.Remove();
foreach (OpenXmlElement endelems in elem)
{
if (endelems is Text && elems.InnerText == e_Ver1)//e_Ver1 = "(/Ver1)"
{
run = (Run)endelems.Parent;
p = (Paragraph)run.Parent;
p.Remove();
break;
}
else
{
Run d_Run = (Run)endelems.Parent;
Paragraph d_p = (Paragraph)d_Run.Parent;
d_p.RemoveAllChildren();
d_p.Remove();*/
try
{
endelems.Remove();
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
}
}
编辑
尝试在代码中使用(在 endelems.remove() 周围)
System.InvalidOperationException: The Parent of this element is Null
//it also says line 141 but I'm not sure how to get line numbering in vs2010
尝试捕获整个事物的错误
System.NullReferenceException: Object reference not set to an instance of an object
//line 114 which would be Paragraph p = (Paragraph)run.Parent; line