XmlDocument xmlDocument=new XmlDocument(); XmlElement root = xmlDocument.CreateElement("", "root", ""); xmlDocument.AppendChild(root); XmlNode rootnode = xmlDocument.SelectSingleNode("root"); for (int i = 0; i < table.Rows.Count; i++) { XmlElement item = xmlDocument.CreateElement("item"); item.SetAttribute("number", table.Rows[i]["number"].ToString()); item.SetAttribute("name", table.Rows[i]["name"].ToString()); item.SetAttribute("specification", table.Rows[i]["specification"].ToString()); item.SetAttribute("comments", table.Rows[i]["comments"].ToString()); root.AppendChild(item); } string xmlstr = xmlDocument.InnerXml;
以上是C# xml的写入方法,从数据库表读取的数据,写入xml中,保存为字符串格式。