可以将XSLT样式表添加到序列化的XML文档中吗?

JL.*_*JL. 19 c# xml

我有一个代码将一个复杂的对象序列化为XML并将其保存为文件,是否有一种快速的方法在序列化过程中在xml中包含样式表?

使用C#和.net框架v2.

bru*_*nde 30

你可以使用XmlWriterWriteProcessingInstruction:

    XmlSerializer s = new XmlSerializer(typeof(myObj));
    using (XmlWriter w = XmlWriter.Create(@"c:\test.xml"))
    {
        w.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"USED-FILE.xsl\"");
        s.Serialize(w, myObj);
    }
Run Code Online (Sandbox Code Playgroud)