我有一个从中读取的字符串:

TextReader tr = new StreamReader(this.dataPath );
string contents = tr.ReadToEnd();


内容的值开头为:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n....."


当我尝试执行

        string styleSheet = "<?xml-stylesheet type=\"text/xsl\" href=\"message.xsl\"?>";
        string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        TextReader tr = new StreamReader(this.dataPath );
        string contents = tr.ReadToEnd();
        contents.Replace(xmlString,xmlString + styleSheet );


绝对不会找到XmlString的首次出现。有什么想法吗?

最佳答案

尝试

contents = contents.Replace(xmlString,xmlString + styleSheet );


这是因为String类是不可变的。

10-08 11:20