标签:
之前未使用过C#,因为在实习单位使用数据库,需要用到C#,故学习了一些,尽是皮毛,略写,权当总结和记录。
我要用到的是,通过程序实现数据的存放,并存放在xml文档里,一开始的教程,全无用处,看书不实践,等于白学。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace CreateXml { class Program { static void Main(string[] args) { Program app = new Program(); app.CreateXmlFile(); } public void CreateXmlFile() { XmlDocument xmlDoc = new XmlDocument(); //创建类型声明节点 //XmlNode node=xmlDoc.CreateXmlDeclaration("1.0","utf-8",""); //xmlDoc.AppendChild(node); //创建根节点 XmlNode root = xmlDoc.CreateElement("annotation"); xmlDoc.AppendChild(root); CreateNode(xmlDoc,root, "folder", "VOC2007"); CreateNode(xmlDoc, root, "filename", "000001.jpg"); XmlNode node3 = xmlDoc.CreateNode(XmlNodeType.Element, "source", null); CreateNode(xmlDoc, node3, "database", "The VOC2007 Database"); CreateNode(xmlDoc, node3, "annotation", "PACAL VOC2007"); CreateNode(xmlDoc, node3, "image", "flickr"); CreateNode(xmlDoc, node3, "flickrid", "341012865"); root.AppendChild(node3); XmlNode node4 = xmlDoc.CreateNode(XmlNodeType.Element, "owner", null); CreateNode(xmlDoc, node4, "flickrid", "Fried Camels"); CreateNode(xmlDoc, node4, "name", "Jinky the Fruit Bat"); root.AppendChild(node4); XmlNode node5 = xmlDoc.CreateNode(XmlNodeType.Element, "size", null); CreateNode(xmlDoc, node5, "width", "353"); CreateNode(xmlDoc, node5, "heigh"," 500"); CreateNode(xmlDoc, node5, "depth", "3"); root.AppendChild(node5); CreateNode(xmlDoc, root, "segmented", "0"); //多一层节点 XmlNode node7 = xmlDoc.CreateNode(XmlNodeType.Element, "object", null); CreateNode(xmlDoc, node7, "name", "dog"); CreateNode(xmlDoc, node7, "pose", "Left"); CreateNode(xmlDoc, node7, "truncated", " 1"); CreateNode(xmlDoc, node7, "difficult", " 0"); XmlNode node70 = xmlDoc.CreateNode(XmlNodeType.Element, "obndbox", null); CreateNode(xmlDoc, node70, "xmin"," 48"); CreateNode(xmlDoc, node70, "ymin", "240"); CreateNode(xmlDoc,node70,"xmax","195"); CreateNode(xmlDoc, node70, "ymax", "371"); node7.AppendChild(node70); root.AppendChild(node7); XmlNode node8 = xmlDoc.CreateNode(XmlNodeType.Element, "object", null); CreateNode(xmlDoc, node8, "name", "person"); CreateNode(xmlDoc, node8, "pose", "Left"); CreateNode(xmlDoc, node8, "truncated", "1"); CreateNode(xmlDoc, node8, "difficult", "0"); XmlNode node80 = xmlDoc.CreateNode(XmlNodeType.Element, "object", null); CreateNode(xmlDoc, node80, "xmin", " 8"); CreateNode(xmlDoc, node80, "ymin", "12"); CreateNode(xmlDoc, node80, "xmax", "352"); CreateNode(xmlDoc, node80, "ymax", "498"); node8.AppendChild(node80); root.AppendChild(node8); try { xmlDoc.Save("C:/Users/`/Desktop/my.xml");//保存路径 } catch (Exception e) { //显示错误信息 Console.WriteLine(e.Message); } //Console.ReadLine(); } /// <summary> /// 创建节点 /// </summary> /// <param name="xmldoc"></param> xml文档 /// <param name="parentnode"></param>父节点 /// <param name="name"></param> 节点名 /// <param name="value"></param> 节点值 /// public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentNode.AppendChild(node); } } }
最后生成:
<annotation> <folder>VOC2007</folder> <filename>000001.jpg</filename> <source> <database>The VOC2007 Database</database> <annotation>PACAL VOC2007</annotation> <image>flickr</image> <flickrid>341012865</flickrid> </source> <owner> <flickrid>Fried Camels</flickrid> <name>Jinky the Fruit Bat</name> </owner> <size> <width>353</width> <heigh>500</heigh> <depth>3</depth> </size> <segmented>0</segmented> <object> <name>dog</name> <pose>Left</pose> <truncated>1</truncated> <difficult>0</difficult> <obndbox> <xmin>48</xmin> <ymin>240</ymin> <xmax>195</xmax> <ymax>371</ymax> </obndbox> </object> <object> <name>person</name> <pose>Left</pose> <truncated>1</truncated> <difficult>0</difficult> <object> <xmin>8</xmin> <ymin>12</ymin> <xmax>352</xmax> <ymax>498</ymax> </object> </object> </annotation>
标签:
原文地址:http://www.cnblogs.com/the-second-coffee/p/5768037.html