zoukankan      html  css  js  c++  java
  • 带命名空间的XML文件的节点添加

    C# Code:

    string xmlPath = "D:\test.xml";
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(xmlPath);
    // Save namespace uri
    string msoUri = "http://schemas.microsoft.com/office/2009/07/customui";
    // Set new namespace
    XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
    xnm.AddNamespace("mso", msoUri);
    XmlNode root = xmlDoc.SelectSingleNode("mso:customUI", xnm);
    XmlElement rootElement = (XmlElement)root;
    rootElement.SetAttribute("xmlns:x1", "ns.iStamp");
    rootElement.SetAttribute("xmlns:x2", "mhbAddin.Connect");
    
    XmlNode node = xmlDoc.SelectSingleNode("mso:customUI/mso:ribbon/mso:tabs", xnm);
    // Create "mso:tabs"
    if (node == null)
    {
        XmlNode ribbon = xmlDoc.SelectSingleNode("mso:customUI/mso:ribbon", xnm);
        XmlElement tabs = xmlDoc.CreateElement("mso", "tabs", msoUri);
        ribbon.AppendChild(tabs);
        node = xmlDoc.SelectSingleNode("mso:customUI/mso:ribbon/mso:tabs", xnm);
    }
    
    // Create "mso:tab" and "mso:group"
    XmlElement tabx1 = xmlDoc.CreateElement("mso", "tab", msoUri);
    tabx1.SetAttribute("idQ", "x1:LegacyIDRemover");
    tabx1.SetAttribute("visible", "false");
    XmlElement group1 = xmlDoc.CreateElement("mso", "group", msoUri);
    group1.SetAttribute("idQ", "x1:grRemoveLegacyID");
    group1.SetAttribute("visible", "false");
    tabx1.AppendChild(group1);
    node.AppendChild(tabx1);
    
    XmlElement tabx2 = xmlDoc.CreateElement("mso", "tab", msoUri);
    tabx2.SetAttribute("idQ", "x2:esqTab");
    XmlElement group2 = xmlDoc.CreateElement("mso", "group", msoUri);
    group2.SetAttribute("x1", "grRemoveLegacyID");
    tabx2.AppendChild(group2);
    node.AppendChild(tabx2);
    
    // Save to file
    xmlDoc.Save(xmlPath);

    test.xml(结果):

    <mso:customUI xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui" xmlns:x1="ns.iStamp" xmlns:x2="mhbAddin.Connect">
      <mso:ribbon>
        <mso:tabs>
          <mso:tab idQ="x1:LegacyIDRemover" visible="false">
            <mso:group idQ="x1:grRemoveLegacyID" visible="false" />
          </mso:tab>
          <mso:tab idQ="x2:esqTab">
            <mso:group x1="grRemoveLegacyID" />
          </mso:tab>
        </mso:tabs>
      </mso:ribbon>
    </mso:customUI>
  • 相关阅读:
    Docker之OVS网络
    Docker Weave网络部署
    Docker Macvlan网络部署
    eclipse下启动tomcat项目,访问tomcat默认端口显示404错误
    eclipse tomcat add时提示The Tomcat server configuration at ServersTomcat v6.0 Server at localhost-
    ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法
    关于TAR ZXVF命令解释
    Linux(UBUNTU) 下安装Eclipse
    spring4 之 helloworld
    Java中Bean是什么
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10406228.html
Copyright © 2011-2022 走看看