求解 長話短說,不要在你的XSL轉換中使用CDATA。詳情請閱讀。C#XSL轉換中斷編碼
背景 標準C#應用程序使用XmlSerializer將對象保存到XML文件。我需要抓住的是保存XML文件,將引用添加到XSL轉換,應用所述變換,然後另存爲.graphml
問題 目前我的輸出似乎有不正確的編碼。該<和>符號表示爲「& LT」和「& GT」,這是無效的graphml(或XML爲此事)
詳細 這裏是輸出文件看起來像此刻:
<?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="n2"/>
<edge source="n1" target="n3"/>
<edge source="n1" target="n5"/>
<edge source="n1" target="n7"/>
<edge source="n1" target="n9"/>
<node id="n2"/>
<edge source="n2" target="n1"/>
<edge source="n2" target="n9"/>
對於它的價值,我知道,我正在轉換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>
我看不出有什麼毛病在C#以外,有三條線之後註釋'/ /添加樣式表關聯',似乎不使用和創建其他代碼中使用的任何對象。但是使用'XslCompiledTransform'' Load'和'Transform'無論如何都是無關緊要的,似乎沒有問題。所以我們需要看到一個最小的XML輸入和XSLT樣式表來說明什麼是錯的。 –
@Martin_Honnen我已經添加了XML和XSLT樣式表 – trademark
您正在使用CDATA,因此這是預期的輸出。 –