xml:
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" (...) >
<w:body>
<w:tbl>
(...)
</w:tbl>
<w:sdt>
(...)
</w:sdt>
vvvvvvvv
<w:p w14:paraId="7A11D" w14:textId="7777777" w:rsidR="003B12D0" w:rsidRDefault="003B12D0" w:rsidP="003B12D0">
<w:pPr>
<w:rPr>
<w:sz w:val="18" />
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="18" />
</w:rPr>
<w:br w:type="page" />
</w:r>
<w:r>
<w:t />
</w:r>
</w:p>
^^^^^^
<w:sectPr w:rsidR="0094346E" w:rsidRPr="000947D4" w:rsidSect="009B7919">
(...)
</w:sectPr>
</w:body>
</w:document>
c#:
XDocument doc = outputDocument.Document;
XNamespace nameSpace = XNamespace.Get("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var result = doc.Descendants().Select(x => x.Element(nameSpace "p"));
This code returns me all the elements containing the < p > tag. It would be cool if I could find an element that has a tag:
<w:r>
<w:t />
</w:r>
and delete the entire object with the closest parent. The code above is between vvv and ^^^
CodePudding user response:
XDocument doc = XDocument.Load(filename);
XNamespace nameSpace = XNamespace.Get("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var nodesWithTagP = doc.Descendants().Select(x => x.Element(nameSpace "p")).Where(x => x!=null);
foreach(var nodeWithTagP in nodesWithTagP)
{
var nodesWithTagR = nodeWithTagP.Descendants(nameSpace "r");
foreach (var nodeWithTagR in nodesWithTagR)
{
var nodesWithTagT = nodeWithTagR.Descendants(nameSpace "t");
foreach (var nodeWithTagT in nodesWithTagT)
{
if(nodeWithTagT.Descendants().Count() = 0)
{
//Here you can use nodeWithTagP
nodeWithTagP.Remove();
}
}
}
}
doc.Save(filename);