2016-09-21 55 views
0

求解 長話短說,不要在你的XSL轉換中使用CDATA。詳情請閱讀。C#XSL轉換中斷編碼

背景 標準C#應用程序使用XmlSerializer將對象保存到XML文件。我需要抓住的是保存XML文件,將引用添加到XSL轉換,應用所述變換,然後另存爲.graphml

問題 目前我的輸出似乎有不正確的編碼。該<和>符號表示爲「& LT」和「& GT」,這是無效的graphml(或XML爲此事)

詳細 這裏是輸出文件看起來像此刻:

<?xml version="1.0" encoding="utf-8"?> 

&lt;graphml xmlns="http://graphml.graphdrawing.org/xmlns" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"&gt; 

&lt;graph id="G" edgedefault="undirected"&gt; 

     &lt;node id="n1"/&gt; 

      &lt;edge source="n1" target="n2"/&gt; 

      &lt;edge source="n1" target="n3"/&gt; 

      &lt;edge source="n1" target="n5"/&gt; 

      &lt;edge source="n1" target="n7"/&gt; 

      &lt;edge source="n1" target="n9"/&gt; 

     &lt;node id="n2"/&gt; 

      &lt;edge source="n2" target="n1"/&gt; 

      &lt;edge source="n2" target="n9"/&gt; 

對於它的價值,我知道,我正在轉換XML是有效的(並且以正確的格式),而且我知道,XSL文件是有效的並且工作正常,因爲我可以用它在其他文件就好了。 有關C#應用XSL轉換的方式導致此問題。下面是相關代碼:

// Create an XML writer 
     XmlSerializer writer = new XmlSerializer(typeof(formattedMeshNet)); 

     var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\netTransform.xml"; 
     var transformPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GraphFormatTransform.xsl"; 
     var savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ExportedNetwork.graphml"; 

     System.IO.FileStream file = System.IO.File.Create(path); 

     writer.Serialize(file, saveMe); 
     file.Close(); 

     // Add style sheet association 
     XmlDocument xDoc = new XmlDocument(); 
     var transformAssn = xDoc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"GraphFormatTransform.xsl\""); 
     xDoc.AppendChild(transformAssn); 


     // Load xml just saved 
     System.Xml.Xsl.XslCompiledTransform myXslTransform; 

     myXslTransform = new System.Xml.Xsl.XslCompiledTransform(); 

     myXslTransform.Load(transformPath); 
     myXslTransform.Transform(path, savePath); 

任何想法,將不勝感激!隨意很好地要求,這將如果你看到愉快地提供細節我已經離開了任何東西:)

潛在相關的問題,其並沒有真正告訴我什麼,但你可能會得到一些有用的東西: XSL transformation give me wrong encoding

我也試過在這裏建議的是: The XslCompiledTransform output encoding

謝謝!

**編輯**

每請求時,這裏是完整的XML文檔和相關的XSL轉換

<?xml version="1.0"?> 
<formattedMeshNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <contents> 
    <node> 
     <nodeID>1</nodeID> 
     <physNeighbors> 
     <int>4</int> 
     <int>6</int> 
     <int>7</int> 
     <int>8</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>2</nodeID> 
     <physNeighbors> 
     <int>4</int> 
     <int>11</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>3</nodeID> 
     <physNeighbors> 
     <int>4</int> 
     <int>9</int> 
     <int>10</int> 
     <int>11</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>4</nodeID> 
     <physNeighbors> 
     <int>1</int> 
     <int>2</int> 
     <int>3</int> 
     <int>8</int> 
     <int>11</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>5</nodeID> 
     <physNeighbors> 
     <int>8</int> 
     <int>11</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>6</nodeID> 
     <physNeighbors> 
     <int>1</int> 
     <int>7</int> 
     <int>8</int> 
     <int>10</int> 
     <int>12</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>7</nodeID> 
     <physNeighbors> 
     <int>1</int> 
     <int>6</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>8</nodeID> 
     <physNeighbors> 
     <int>1</int> 
     <int>4</int> 
     <int>5</int> 
     <int>6</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>9</nodeID> 
     <physNeighbors> 
     <int>3</int> 
     <int>11</int> 
     <int>12</int> 
     <int>13</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>10</nodeID> 
     <physNeighbors> 
     <int>3</int> 
     <int>6</int> 
     <int>13</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>11</nodeID> 
     <physNeighbors> 
     <int>2</int> 
     <int>3</int> 
     <int>4</int> 
     <int>5</int> 
     <int>9</int> 
     <int>13</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>12</nodeID> 
     <physNeighbors> 
     <int>6</int> 
     <int>9</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    <node> 
     <nodeID>13</nodeID> 
     <physNeighbors> 
     <int>9</int> 
     <int>10</int> 
     <int>11</int> 
     </physNeighbors> 
     <visited /> 
     <neighbors /> 
    </node> 
    </contents> 
</formattedMeshNet> 


XSL


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:template match="/"> 
    <![CDATA[ 
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
    http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">]]> 

    <![CDATA[<graph id="G" edgedefault="undirected">]]> 
     <xsl:for-each select="/formattedMeshNet/contents/node"> 
     <xsl:variable name="parentNodeID"> 
      <xsl:value-of select="nodeID" /> 
     </xsl:variable> 
      <![CDATA[<node id="n]]><xsl:copy-of select="$parentNodeID" /><![CDATA["/>]]> 
      <xsl:for-each select="physNeighbors/int"> 
       <![CDATA[<edge source="n]]><xsl:copy-of select="$parentNodeID" /><![CDATA[" target="n]]><xsl:value-of select="." /><![CDATA["/>]]> 
      </xsl:for-each> 
     </xsl:for-each> 


    <![CDATA[</graph> 
    </graphml>]]> 
</xsl:template> 
</xsl:stylesheet> 
+0

我看不出有什麼毛病在C#以外,有三條線之後註釋'/ /添加樣式表關聯',似乎不使用和創建其他代碼中使用的任何對象。但是使用'XslCompiledTransform'' Load'和'Transform'無論如何都是無關緊要的,似乎沒有問題。所以我們需要看到一個最小的XML輸入和XSLT樣式表來說明什麼是錯的。 –

+0

@Martin_Honnen我已經添加了XML和XSLT樣式表 – trademark

+0

您正在使用CDATA,因此這是預期的輸出。 –

回答

0

如果我猜正確地說,你的樣式表應該是這樣的:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/formattedMeshNet"> 
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
    http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> 
     <graph id="G" edgedefault="undirected"> 
      <xsl:for-each select="contents/node"> 
       <xsl:variable name="parentNodeID" select="nodeID" /> 
       <node id="n{$parentNodeID}" /> 
       <xsl:for-each select="physNeighbors/int"> 
        <edge source="n{$parentNodeID}" target="n{.}" /> 
       </xsl:for-each> 
      </xsl:for-each> 
     </graph> 
    </graphml> 
</xsl:template> 

</xsl:stylesheet> 

應用到你的輸入例子,結果將是:

<?xml version="1.0" encoding="UTF-8"?> 
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns  http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> 
    <graph id="G" edgedefault="undirected"> 
     <node id="n1"/> 
     <edge source="n1" target="n4"/> 
     <edge source="n1" target="n6"/> 
     <edge source="n1" target="n7"/> 
     <edge source="n1" target="n8"/> 
     <node id="n2"/> 
     <edge source="n2" target="n4"/> 
     <edge source="n2" target="n11"/> 
     <node id="n3"/> 
     <edge source="n3" target="n4"/> 
     <edge source="n3" target="n9"/> 
     <edge source="n3" target="n10"/> 
     <edge source="n3" target="n11"/> 
     <node id="n4"/> 
     <edge source="n4" target="n1"/> 
     <edge source="n4" target="n2"/> 
     <edge source="n4" target="n3"/> 
     <edge source="n4" target="n8"/> 
     <edge source="n4" target="n11"/> 
     <node id="n5"/> 
     <edge source="n5" target="n8"/> 
     <edge source="n5" target="n11"/> 
     <node id="n6"/> 
     <edge source="n6" target="n1"/> 
     <edge source="n6" target="n7"/> 
     <edge source="n6" target="n8"/> 
     <edge source="n6" target="n10"/> 
     <edge source="n6" target="n12"/> 
     <node id="n7"/> 
     <edge source="n7" target="n1"/> 
     <edge source="n7" target="n6"/> 
     <node id="n8"/> 
     <edge source="n8" target="n1"/> 
     <edge source="n8" target="n4"/> 
     <edge source="n8" target="n5"/> 
     <edge source="n8" target="n6"/> 
     <node id="n9"/> 
     <edge source="n9" target="n3"/> 
     <edge source="n9" target="n11"/> 
     <edge source="n9" target="n12"/> 
     <edge source="n9" target="n13"/> 
     <node id="n10"/> 
     <edge source="n10" target="n3"/> 
     <edge source="n10" target="n6"/> 
     <edge source="n10" target="n13"/> 
     <node id="n11"/> 
     <edge source="n11" target="n2"/> 
     <edge source="n11" target="n3"/> 
     <edge source="n11" target="n4"/> 
     <edge source="n11" target="n5"/> 
     <edge source="n11" target="n9"/> 
     <edge source="n11" target="n13"/> 
     <node id="n12"/> 
     <edge source="n12" target="n6"/> 
     <edge source="n12" target="n9"/> 
     <node id="n13"/> 
     <edge source="n13" target="n9"/> 
     <edge source="n13" target="n10"/> 
     <edge source="n13" target="n11"/> 
    </graph> 
</graphml> 
+0

這就是訣竅。我不知道CDATA在這種情況下不起作用。我想我假設,因爲我包裝的數據包含XML標籤,如果我沒有將它放在某種包裝中,它會感到困惑。這真的很簡單。謝謝! – trademark

+1

它不會感到困惑,因爲樣式表指令前綴爲'xsl:',並且該前綴綁定到XSLT名稱空間。這允許處理器區分指令和文字結果元素。 –