2012-04-24 130 views
4

我需要更改按文檔的xml聲明部分,或只選擇數據減去聲明。哪個更容易?更改Xml聲明或選擇xml無聲明部分

這是什麼我的XML看起來像一個例子:

<?xml version="1.0" encoding="utf-16"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster"> 
      <fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in"/> 
      <fo:region-after border-style="none" border-width="thin" extent="0.25in"/> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 
    <fo:page-sequence master-reference="PageMaster"/> 
</fo:root> 

我試圖改變XML聲明是:

<?xml version="1.0" encoding="iso-8859-1"?> 
+0

它的工作原理...嘗試再添加一個。 – ykatchou 2012-04-24 13:37:06

+0

請添加更多格式... – ykatchou 2012-04-24 13:37:45

+0

*爲什麼*您是否在嘗試更改XML聲明? – Tomalak 2012-04-24 13:40:19

回答

7

你是不是想以編程方式更改的XML?如果是這樣,你可以通過創建新的XmlDeclaration並與前一個替換它這樣做,如下圖所示:

  XmlElement element = doc.DocumentElement; 
      XmlDeclaration xmlDeclaration; 
      xmlDeclaration = doc.CreateXmlDeclaration("1.0", "iso-8859-1", null); 
      doc.ReplaceChild(xmlDeclaration, doc.FirstChild); 

你只需要確保該文件的第一個孩子是XML聲明。

0

的通緝令「XML聲明的改變」,可以非常輕鬆地完成(不評論這種變化是否是正確的解決問題的方法),使用XSLT:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" encoding="ISO-8859-1"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

就將此改造於所提供的XML文檔

<?xml version="1.0" encoding="utf-16"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster"> 
      <fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in"/> 
      <fo:region-after border-style="none" border-width="thin" extent="0.25in"/> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 
    <fo:page-sequence master-reference="PageMaster"/> 
</fo:root> 

和想要的結果產生

<?xml version="1.0" encoding="iso-8859-1"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster"> 
      <fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in" /> 
      <fo:region-after border-style="none" border-width="thin" extent="0.25in" /> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 
    <fo:page-sequence master-reference="PageMaster" />