xml创建
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string sql = "select rentid from lesseeRent "; DataTable dt= MDGL.DBUtility.SQLServerHelper.GetDataTable(CommandType.Text,sql); //创建一个xml文档 XmlDocument xmlDoc = new XmlDocument(); //声明,一个节点 XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration,"",""); xmlDoc.AppendChild(xmlNode); //注释 XmlComment xmlComment = xmlDoc.CreateComment("rentID列表"); xmlDoc.AppendChild(xmlComment); //创建根元素 XmlElement xmlEle = xmlDoc.CreateElement("rentIDList"); xmlDoc.AppendChild(xmlEle); //创建子元素 XmlElement xmlEle2; foreach(DataRow dr in dt.Rows) { xmlEle2 = xmlDoc.CreateElement("rentID"); XmlText xmlText = xmlDoc.CreateTextNode(dr["rentID"].ToString()); xmlEle2.AppendChild(xmlText);//子元素添加文本内容 xmlEle.AppendChild(xmlEle2);//父元素添加子元素 } xmlDoc.Save("D:\\b.xml");//xml文档存储
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//写xml string sql = "select rentid from lesseeRent "; DataTable dt= MDGL.DBUtility.SQLServerHelper.GetDataTable(CommandType.Text,sql); //创建一个xml文档 XmlDocument xmlDoc = new XmlDocument(); StringBuilder str = new StringBuilder(); str.Append("<?xml version=\"1.0\" ?>"); str.Append("<rentList>"); foreach (DataRow dr in dt.Rows) { str.Append("<rentID>"); str.Append(dr["rentID"].ToString()); str.Append("</rentID>"); } str.Append("</rentList>"); xmlDoc.LoadXml(str.ToString()); xmlDoc.Save("D:\\a.xml");