使用C#编写未编码的文件

本文关键字:编码 文件 使用 | 更新日期: 2023-09-27 18:29:51

以下代码需要生成一个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

我看不出有什么方法可以只写响应.write期间呈现的相同文本。任何线索都将不胜感激。

使用C#编写未编码的文件

改变您的方法。对不起,我没有注意到你在使用FOR XML子句。

在这种情况下,必须使用ExecuteXmlReader,并保存结果输出。这里有一个例子:

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

希望这能有所帮助!

尝试在输出中使用HtmlDecode方法。http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx

您尝试过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