从XML文档中删除空行

本文关键字:删除 XML 文档 | 更新日期: 2023-09-27 18:17:43

我现在面临一个问题。我在c#中加载xml文件,并从中删除一些节点,并附加一些节点。现在的问题是,当我从XML文件中删除时,会自动创建一些空行,所以我想删除这些行。

当我在xml中添加一些节点到父节点时,我想在每个结束标记

中添加新行例如

。我的Xml文件是

<intro id="S0001">
    <title>Introduction Title</title>
     <para>This is a paragraph. Note that paragraphs can contain other block–level objects, such as lists, as well as directly containing text.</para>
     <para>The introduction can contain all of the text objects that a section can contain, except that it cannot be divided into parts, sections and sub–sections.</para>
     <para>The introduction can contain tables:</para>
   </intro><part>
       <no>Part A</no> Article Structure <sup>&lpar;Part Title&rpar;</sup><section1 id="S0002">`enter code here`
     <no>Sect 1</no>
     <title>First Section in Part 1 <sup>&lpar;Section 1 Title&rpar;</sup></title>
     <shortsectionhead>Short Section Header</shortsectionhead>
     <para>This is a section in the first part of the article.</para>
   </section1><section1 id="S0003">
代码:

XmlNode partNnode = xmlDoc.SelectSingleNode("//part");
XmlNode introNode=xmlDoc.SelectSingleNode("//intro");
XmlDocumentFragment newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=partNnode.OuterXml;
introNode.ParentNode.InsertAfter(newNode,introNode);
partNnode.ParentNode.RemoveChild(partNnode);
partNnode = xmlDoc.SelectSingleNode("//part");
nodeList = xmlDoc.SelectNodes("//section1");    
foreach (XmlNode refrangeNode in nodeList)
{
    newNode=xmlDoc.CreateDocumentFragment();
    newNode.InnerXml=refrangeNode.??OuterXml;
    partNnode.AppendChild(newNode);
} 

请帮帮我提前感谢

从XML文档中删除空行

如果您使用c#加载并保存XMl文件,那么XMl应该被正确格式化(格式化奇怪的XMl文件的简单方法就是使用一些c#代码加载并保存它们)。

如果我正确理解了您的问题,那么您只是对XML文件的格式不满意?

Like you want (A):

</intro><part>

但是你得到(B):

</intro>
<part>

如果这是问题,那么,在我眼里,你只是想要一个奇怪的东西。因为…

a)代码不关心XML文件是如何格式化的b) (b)中的格式是正确的

如果出于某种原因想要更改它,则必须解析整个XML文件,将其作为字符串打开,并手动检查关闭和打开的标记。