• 一个是对于带有命名空间的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");
            }
  • 相关阅读:
    sql 数据库还原脚本 (kill链接+独占
    最长回文字符串
    UVa 455 Periodic Strings
    UVa 1225 Digit Counting
    UVa 340 Master-Mind Hints
    UVa 10976
    UVa 725
    UVa 11059
    POJ1887 最长下降子序列
    最大连续子序列算法(数组的连续子数组最大和(首尾不相连))
  • 原文地址:https://www.cnblogs.com/snowball/p/536504.html
Copyright © 2020-2023  润新知