简体   繁体   English

如何使用C#4.0从xml内容中删除特定的xml元素?

[英]how to remove specific xml elements from the xml content using C#4.0?

This is xml content. 这是xml内容。

<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. 实际上,此xml内容是通过html到ooxml转换器[HtmlToOpenXml.dll]生成的。但是它错误地在每个<w:tc>元素的末尾添加了两个<w:p>元素。因此,我想删除那些<w:p>转换器生成的xml内容中的<w:p>元素。我具有字符串格式的xml内容。

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. 如果一切都那么简单,我强烈建议您使用@sylon的答案。 Anyway, this is a simple example of how to do this with LINQ to XML: 无论如何,这是一个简单的示例,说明如何使用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. 如果需要删除一些更特定的标记,那么Where子句可以包含更特定的条件。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM