• 带命名空间的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>
  • 相关阅读:
    通过Math.atan2计算角度 改变物体朝向
    table.sort 排序的问题
    shader 实现正旋波效果 水面波动效果
    第一篇碎碎心得
    ping 整理
    路由器
    C语言里如何读取位数据的某几位?
    ubunut命令
    基于SPIflash 的fatfs调试步骤
    makefile 学习总结--函数
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10406228.html
Copyright © 2020-2023  润新知