XmlDocument,XmlNode,XmlElement创建复杂XML文档

#region 生成XML消息
                    XmlDocument xmlDoc 
= new XmlDocument();


                    
//生成描述版本
                    XmlDeclaration xmlDec;
                    xmlDec 
= xmlDoc.CreateXmlDeclaration("1.0""gb2312""yes");
                    xmlDoc.InsertBefore(xmlDec, xmlDoc.DocumentElement);

                    
//定义根节点
                    XmlElement element;
                    XmlNode rootNode;
                    XmlNode subNode;
                    element 
= xmlDoc.CreateElement("message");
                    element.SetAttribute(
"System""ATMS");
                    element.SetAttribute(
"Ver""1.0");
                    rootNode 
= xmlDoc.AppendChild(element);

                    
//定义子系统
                    element = xmlDoc.CreateElement("systemtype");
                    element.InnerText 
= SubType;
                    rootNode.AppendChild(element);

                    
//定义消息类型
                    element = xmlDoc.CreateElement("messagetype");
                    element.InnerText 
= "ALARM";
                    rootNode.AppendChild(element);

                    
//定义子系统
                    element = xmlDoc.CreateElement("systemtype");
                    element.InnerText 
= SubType;
                    rootNode.AppendChild(element);

                    
//发出消息的源ip地址
                    element = xmlDoc.CreateElement("sourceIP");
                    element.InnerText 
= "";
                    rootNode.AppendChild(element);

                    
//接收消息的目的ip地址
                    element = xmlDoc.CreateElement("sourceIP");
                    element.InnerText 
= "";
                    rootNode.AppendChild(element);

                    
//用户名
                    element = xmlDoc.CreateElement("user");
                    element.InnerText 
= "";
                    rootNode.AppendChild(element);

                    
//密码
                    element = xmlDoc.CreateElement("password");
                    element.InnerText 
= "";
                    rootNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("Alarm");
                    element.InnerText 
= "";
                    rootNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("type");
                    element.InnerText 
= "";
                    rootNode.LastChild.AppendChild(element);

                    element 
= xmlDoc.CreateElement("device");
                    element.InnerText 
= "";
                    subNode
=rootNode.LastChild.AppendChild(element);

                    element 
= xmlDoc.CreateElement("id");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("name");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("location");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("errortype");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("occurtime");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("releasetime");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    element 
= xmlDoc.CreateElement("desc");
                    element.InnerText 
= "";
                    subNode.AppendChild(element);

                    
string x = xmlDoc.OuterXml;
                    
#endregion
posted @ 2009-08-04 19:06  PointNet  阅读(2342)  评论(0编辑  收藏  举报