I'm trying to parse through a lengthy file and remove sections that I don't want. From research It appears that the OpenXml SDK was the easiest reference for manipulating and searching through the word doc. Unfortunately, it's not always consistent because I keep getting NullReferenceExceptions
when trying to assign nodes like to a run object. Essentially, my program should go through the docx file and find the tags (ver 1) and then remove everything in between it and the closing tag (/ver 1). this only seems to work for some section as other sections I get the NullReferenceException
and I feel it has to do with the messy formatting that MS Word uses but I don't know.
Here's the code for a particular section if anyone could help I'd Appreciate it.
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());
}
}
}
}
}
Edit
try catch with in the code ( around the 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
try catch error around entire thing
System.NullReferenceException: Object reference not set to an instance of an object
//line 114 which would be Paragraph p = (Paragraph)run.Parent; line
I am not quite sure what you are trying to do here, but...
You get a static list of children from the body.
You iterate over possibly deleted children. And then call remove a child that were already removed with RemoveAllChildren()
.
Not to mention this faulty logic.
if (endelems is Text && elems.InnerText == e_Ver1)//e_Ver1 = "(/Ver1)"
{
...
else
{
Run d_Run = (Run)endelems.Parent;
}
In the else clause, endelems will probably not have a parent that is a Run
, since it probably wouldn't be a Text
element.
--- EDIT --- pseudocode
IEnumerable<Text> elems = wd.MainDocumentPart.Document.Body.Descendants<Text>();
foreach (Text elem in elems)
{
if(elem.InnerText.Equals("Ver 1"))
{
IEnumerable<OpenXmlElement> afterelems = elem.ElementsAfter();
foreach(OpenXmlElement openelem in afterelems)
{
if(openelem is Text && ((Text)openelem).InnerText.Equals("Ver 2"))
{
break;
} else if(openelem is Text) {
openelem.Remove();
}
}
break;
}
}
foreach (Run run in wd.MainDocumentPart.Document.Body.Descendants<Run>().Where(run => run.Descendants<Text>().Count() == 0 && run.Descendants<Break>().Count() == 0))
{
run.Remove();
}
foreach (Paragraph par in wd.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where(par => par.Descendants<Run>().Count() == 0 && par.Descendants<Table>().Count() == 0))
{
par.Remove();
}