zoukankan      html  css  js  c++  java
  • 一个是对于带有命名空间的xml进行操作

    今天想写一个可以生成excel xml 格式的类库,可是对于xml的操作语法不熟悉,所以试了又试,
    一个是替换节点
    一个是对于带有命名空间的xml进行操作。


    static void XmlTest1()
            
    {
                
    //替换节点
                System.Xml.XmlDocument doc = new XmlDocument();
                doc.AppendChild(doc.CreateElement(
    "Root"));
                doc.DocumentElement.AppendChild(doc.CreateElement(
    "sub"));
                XmlNode node1 
    = doc.CreateElement("sub1");

                System.Xml.XmlDocument doc2 
    = new XmlDocument();
                XmlNode node2 
    = doc2.CreateElement("sub2");

                doc.DocumentElement.ReplaceChild(node1,doc.DocumentElement.SelectSingleNode(
    "sub"));
                
    //doc.DocumentElement.ReplaceChild(node2,doc.DocumentElement.SelectSingleNode("sub"));
                
                doc.Save(
    "xml.xml");
            }

            
    static void XmlTest2()
            
    {
                
    //对具有命名空间的xml进行操作
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                System.Xml.XmlNamespaceManager xm 
    = new System.Xml.XmlNamespaceManager(doc.NameTable);            
                xm.AddNamespace(
    "o","urn:schemas-microsoft-com:office:office");
                xm.AddNamespace(
    "x","urn:schemas-microsoft-com:office:excel");
                xm.AddNamespace(
    "s","urn:schemas-microsoft-com:office:spreadsheet");
                doc.AppendChild(doc.CreateXmlDeclaration(
    "1.0","",""));
                doc.AppendChild(doc.CreateProcessingInstruction(
    "mso-application","progid=\"Excel.Sheet\""));
                
                System.Xml.XmlNode  node 
    = doc.CreateNode(System.Xml.XmlNodeType.Element,"s","RootNode",xm.LookupNamespace("s"));

                XmlAttribute NewAttribute 
    = doc.CreateAttribute("xmlns:x");
                NewAttribute.Value 
    = xm.LookupNamespace("x");
                node.Attributes.SetNamedItem(NewAttribute);

                NewAttribute 
    = doc.CreateAttribute("xmlns:o");
                NewAttribute.Value 
    = xm.LookupNamespace("o");
                node.Attributes.SetNamedItem(NewAttribute);
                
                doc.AppendChild(node);            
                doc.DocumentElement.AppendChild(doc.CreateElement(
    "s","test",xm.LookupNamespace("s")));
                doc.DocumentElement.AppendChild(doc.CreateElement(
    "x","test",xm.LookupNamespace("x")));
                doc.Save(
    "xml.xml");

                XmlNode  objNode 
    = doc.DocumentElement.SelectSingleNode("s:test",xm);
            }


    static void XmlTest1()
            
    {
                
    //替换节点
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.AppendChild(doc.CreateElement(
    "Root"));
                doc.DocumentElement.AppendChild(doc.CreateElement(
    "sub"));
                XmlNode node1 
    = doc.CreateElement("sub1");

                System.Xml.XmlDocument doc2 
    = new XmlDocument();
                XmlNode node2 
    = doc2.CreateElement("sub2");

                
    //doc.DocumentElement.ReplaceChild(node1,doc.DocumentElement.SelectSingleNode("sub"));

                System.Xml.XmlNode  node3 
    = doc.ReadNode(new System.Xml.XmlTextReader(new System.IO.StringReader(node2.OuterXml)));
                doc.DocumentElement.ReplaceChild(node3,doc.DocumentElement.SelectSingleNode(
    "sub"));
                doc.DocumentElement.AppendChild(node3);
                
                doc.Save(
    "xml.xml");
            }
  • 相关阅读:
    二分图 洛谷P2055 [ZJOI2009]假期的宿舍
    并查集 洛谷P1640 [SCOI2010]连续攻击游戏
    贪心 洛谷P2870 Best Cow Line, Gold
    贪心 NOIP2013 积木大赛
    快速幂 NOIP2013 转圈游戏
    倍增LCA NOIP2013 货车运输
    树形DP 洛谷P2014 选课
    KMP UVA1328 Period
    动态规划入门 BZOJ 1270 雷涛的小猫
    KMP POJ 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/snowball/p/536504.html
Copyright © 2011-2022 走看看