• c#中如何在xml中添加和替换xsl样式表


    在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);

  • 相关阅读:
    钓鱼
    Webpack3.x升级至 4.x 小记
    设计模式六大原则(总结)
    Spring Data RedisTemplate抛出SerializationException
    pcap文件过滤、分割、合并处理工具
    如何查看Linux服务器的负载
    java8 List按照两个属性第一个属性升序排序第二个属性降序排序
    什么是云原生
    vuecli创建vue3项目
    nfs 问题汇总
  • 原文地址:https://www.cnblogs.com/halia/p/2184777.html
Copyright © 2020-2023  润新知