2011-12-14 87 views
0

我目前遇到問題。我正在C#中加載一個xml文件,並從中刪除一些節點並添加一些節點。現在的問題是,當我從XML文件中刪除然後有一些空行自動創建,所以我想刪除這些行。從XML文檔中刪除空行

當我一些節點追加到XML中的父節點的話,我想在每個結束標籤

EG的新生產線。我的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); 
} 

請幫我提前

+0

你可以發佈你迄今爲止嘗試過的代碼嗎? – 2011-12-14 10:11:41

回答

0

感謝如果您加載和保存使用C#中的XML文件,然後將XML應該正確格式化(一種簡單的方法來格式化奇怪的外觀XML文件只是加載和保存一些C#代碼)。

如果我正確理解你的問題,那麼你只是不滿意XML格式的文件?

像你想(A):

</intro><part> 

但是你(B):

</intro> 
<part> 

如果是這樣的問題,那麼,在我眼裏,你只是想要一個奇怪的事情。因爲...

一)的代碼,並不關心XML文件是如何被格式化和 二)(B)是正確的

如果,原因是什麼永遠的格式,想改變它,那麼你必須解析XML文件,打開它作爲一個字符串,並手動檢查關閉和打開的標籤。