긴 파일을 파싱하고 싶지 않은 섹션을 제거하려고합니다. 연구 결과 OpenXml SDK는 doc라는 단어를 통해 조작하고 검색 할 때 가장 쉬운 참조였습니다. 불행하게도 노드를 실행 오브젝트와 같이 할당하려고 시도 할 때 계속해서 NullReferenceExceptions
을 얻으므로 일관성있는 것은 아닙니다. 본질적으로, 내 프로그램은 docx 파일을 통해 가서 태그 (ver 1)를 찾은 다음 닫는 태그 (/ ver 1) 사이의 모든 것을 제거해야합니다. 이 부분은 내가 다른 부분으로 작동하는 것으로 보입니다. NullReferenceException
이 있는데 MS Word가 사용하는 지저분한 형식화와 관련이 있다고 느낍니다. 그러나 모르겠습니다.DOCX 파일에서 OpenXML을 사용하는 상수 NullReference 예외
누군가가 내가 그것을 감사하게 도울 수있는 경우 다음은 특정 섹션 코드입니다. 모든 게 주위의 코드와
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
것 같아요 것이다 어딘가에 그'elems.Parent' 또는'endelems.Parent'는'null' 값을 반환합니다. 최종적으로 루트 노드를 나타낼 수 있습니까? 편집 : 왜 당신이 디버거를 통해 실행하고 왜/실패 이유를보고, 또는 적어도 우리에게 더 많은 정보를 제공합니다. –
어떤 라인에서 예외가 발생합니까? – Romoku
모든 것을 try-catch로 둘러 싸고 오류를 게시 할 수 있습니까? – devilfish17