This is xml content.
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>1</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>2</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>3</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>4</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
</w:tr>
</w:tbl>
Actually this xml content are generated by html to ooxml converter[HtmlToOpenXml.dll].But it wrongly added the two <w:p>
elements at end of the every <w:tc>
elements.So, i want to remove those <w:p>
elements from the xml content generated by the converter.I have the xml content in string format.
Please guide me to get out of this issue...
您可以使用字符串替换吗?
xmlString.Replace("<w:p />", "");
If everything is that easy, I strongly suggest using the answer by @sylon. Anyway, this is a simple example of how to do this with LINQ to XML:
XElement x = XElement.Load("In.xml");
string prefix = "w";
XNamespace w = x.GetNamespaceOfPrefix(prefix);
var ds = x.Descendants(w + "p")
.Where(d => string.IsNullOrEmpty(d.Value));
ds.Remove();
x.Save("Out.xml");
The Where
clause can contain more specific conditions, if you needed to remove some more specific tags.
string xmlString = @"<w:tbl xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>1</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>2</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>3</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>4</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
</w:tr>
</w:tbl>";
XDocument doc = XDocument.Parse(xmlString);
doc.Root.Descendants().Where(d => d.IsEmpty && !d.HasAttributes).Remove();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.