zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    Ansiable Manage MySQL global variables
    Ansible 从MySQL数据库添加或删除用户
    Ansible 管理MySQL主从复制
    Ansible 从远程主机添加或删除MySQL数据库
    vi/vim编辑器
    shell doc
    ubuntu 上 SSH scp 技巧
    SpringBoot 整合 devtools 实现热部署
    Gson 解决时间解析问题
    springboot retry
  • 原文地址:https://www.cnblogs.com/halia/p/2184777.html
Copyright © 2011-2022 走看看