首页 > 代码库 > 创建XML文件

创建XML文件

//创建XML文件
             XmlDocument xmldoc = new XmlDocument();
            XmlText xmltext;
             XmlNode xmlnode = xmldoc.CreateXmlDeclaration( "1.0", "utf-8" , null );
             xmldoc.AppendChild(xmlnode);
           
             XmlElement xmlcast = xmldoc.CreateElement( "", "Cast","" );
              xmltext = xmldoc.CreateTextNode( "");
             xmlcast.AppendChild(xmltext);
             xmldoc.AppendChild(xmlcast);

             XmlElement xmlsend = xmldoc.CreateElement( "", "SendUserId", "" );
             xmltext = xmldoc.CreateTextNode( WangCaiConfig.User.Id.ToString());
             xmlsend.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmlsend);

             XmlElement xmlmessage = xmldoc.CreateElement( "", "Message", "" );
             xmltext = xmldoc.CreateTextNode(content);
             xmlmessage.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmlmessage);

             XmlElement xmlsubect = xmldoc.CreateElement( "", "Subject", "" );
             xmltext = xmldoc.CreateTextNode(title);
             xmlsubect.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmlsubect);

             XmlElement xmlstate = xmldoc.CreateElement( "", "State", "" );
             xmltext = xmldoc.CreateTextNode( "1");
             xmlstate.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmlstate);

             XmlElement xmltime = xmldoc.CreateElement( "", "SendTime", "" );
             xmltext = xmldoc.CreateTextNode(time.ToString());
             xmltime.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmltime);

             XmlElement xmlUserList = xmldoc.CreateElement( "", "UserList", "" );
             xmltext = xmldoc.CreateTextNode( "");
             xmlUserList.AppendChild(xmltext);
             xmldoc.ChildNodes.Item(1).AppendChild(xmlUserList);

             string[] str = acceptUserId.Split( new char[] { ‘&‘ });
             for ( int i = 0; i < str.Length; i++)
             {
                 XmlElement xmlaccept = xmldoc.CreateElement( "", "UserId", "" );
                 xmltext = xmldoc.CreateTextNode(str[i].ToString());
                 xmlaccept.AppendChild(xmltext);
                 xmldoc.ChildNodes.Item(1).AppendChild(xmlUserList).AppendChild(xmlaccept);
             }
            xmldoc.Save( WangCaiConfig.GetCurrentDirectory()+"UserXML/BoradCast.xml" );

创建出来的样式
<?xml version="1.0" encoding =" utf-8" ?>
<Cast>
  <SendUserId> 3</ SendUserId>
  <Message> sasdasd</ Message>
  <Subject> 3244</ Subject>
  <State> 1</ State>
  <SendTime> 2014/8/29 14:23:50</SendTime >
  <UserList>
    <UserId> 42</ UserId>
    <UserId> 43</ UserId>
  </UserList>
</Cast>

创建XML文件