Try again

200512270453934121.gif

博客园 首页 联系 管理
今天想写一个可以生成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");
        }
posted on 2006-10-22 16:39  共同学习,共同进步  阅读(697)  评论(1编辑  收藏  举报