在xml文件中,如果要引入样式表xsl,就必须有这样一句:<?xml-stylesheet type="text/xsl" href="sample.xsl"?>

如果我们要用c#来添加样式表,那么方法如下:

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);

            // Add xsl style to the .xml
            XmlProcessingInstruction xmlXsl = doc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"sample.xsl\""); //调用CreateProcessingInstruction方法
            doc.AppendChild(xmlXsl);
 

    doc.Save(xmlFile);

如果我们要对xml中已有的样式表进行替换,那么可以采用如下方法:

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);
            XmlNode xmlNode = doc.SelectSingleNode("/processing-instruction('xml-stylesheet')"); //Select the old stylesheet
            //Replace xsl stylesheet to the .xml 

            XmlProcessingInstruction xmlXsl = doc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"newstyle.xsl\"");
            doc.ReplaceChild(xmlXsl, xmlNode); //调用replacechild方法来用新样式表替换旧样式
            doc.Save(xmlFile);

相关文章:

  • 2022-12-23
  • 2021-12-29
  • 2022-02-02
  • 2022-12-23
  • 2021-10-18
  • 2021-10-20
  • 2021-06-16
猜你喜欢
  • 2022-12-23
  • 2021-06-16
  • 2021-06-07
  • 2022-02-18
  • 2022-12-23
  • 2021-12-27
  • 2022-01-20
相关资源
相似解决方案