簡體   English   中英

使用C#寫入未編碼的文件文件

[英]Write unencoded File file using C#

以下代碼需要產生一個XML文件。

string path = @"c:\load\myFile.xml";
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpFloors"].ConnectionString);
        SqlCommand cmd;
        cmd = new SqlCommand("select coords.xyid as '@id', xmin, xmax, ymin, ymax,(select brief, long, img from floordesc where coords.xyid = floordesc.xyid for xml path(''),Type) as 'desc' from coords where xyid <> '' for xml path('coord'), Elements XSINIL,root('coords')", connection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet("coords");
        da.Fill(ds, "coord");
        Response.Write(ds.GetXml());
        File.WriteAllText(path, ds.GetXml());

XML看起來應該像這樣:

<?xml version="1.0" encoding="iso-8859-1"?>
<coords>
<coord id="6090">
    <title>Office 6090</title>
    <xmin>10</xmin>
    <xmax>60</xmax>
    <ymin>40</ymin>
    <ymax>90</ymax>
    <desc>
        <brief>one</brief>
        <long>one more</long>
        <img>dude.jpg</img>
    </desc>
</coord>
<coord id="11090">....

Response.Write代碼將其正確寫出到頁面中。 但是,當我嘗試寫入文件時,它被更改為htmlencoding。

 &lt;coords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt

我看不到一種方法來只寫在response.write期間呈現的相同文本。 任何線索將不勝感激。

改變你的方法。 抱歉,您沒有使用FOR XML子句。

在這種情況下,必須使用ExecuteXmlReader並保存結果輸出。 這是一個例子:

var xmlReader = cmd.ExecuteXmlReader();
var xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
xmlReader.Close();
xmlDoc.Save(path);

希望這可以幫助!

您是否嘗試過ds.WriteXml(string path)

參考: http : //msdn.microsoft.com/en-us/library/hb27k0tf.aspx


如果可以的話,您可以繞過從.net寫入文件並在sql端進行操作

bcp "SELECT * FROM pubs..authors FOR XML RAW" queryout c:\myfile.xml -Sserver -Uusername -Ppassword -c -r -t

參考: http : //www.sqlteam.com/forums/topic.asp? TOPIC_ID= 9336

嘗試在輸出上使用HtmlDecode方法。 http://msdn.microsoft.com/zh-CN/library/7c5fyk1k.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM