#region 自动生成xml文件 public void GenerateXML(string path, string filename, Mat mat, PredictResult result) { XmlDocument xmlDoc = new XmlDocument(); //创建类型声明节点 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); xmlDoc.AppendChild(node); //创建根节点 XmlElement xRoot = xmlDoc.CreateElement("annotation"); // //给节点属性赋值 // xeRoot.SetAttribute("version", "1.0"); // xeRoot.SetAttribute("name", "商品数据"); xmlDoc.AppendChild(xRoot); CreateNode(xmlDoc, xRoot, "folder", "危险品车图片"); CreateNode(xmlDoc, xRoot, "filename", filename); CreateNode(xmlDoc, xRoot, "path", path); // CreateNode(xmlDoc, xRoot, "source", ""); XmlElement xSource = xmlDoc.CreateElement("source"); //子节点 XmlNode xn_Root = xmlDoc.SelectSingleNode("annotation");//找到根节点node xn_Root.AppendChild(xSource);//父级节点添加子节点 CreateNode(xmlDoc, xSource, "database", "Unknown"); XmlElement xSize = xmlDoc.CreateElement("size"); xn_Root.AppendChild(xSize); CreateNode(xmlDoc, xSize, "width", mat.Width.ToString()); CreateNode(xmlDoc, xSize, "height", mat.Height.ToString()); CreateNode(xmlDoc, xSize, "depth", "3"); XmlElement xSegmented = xmlDoc.CreateElement("segmented"); xSegmented.InnerText = "0"; xn_Root.AppendChild(xSegmented); for (int i = 0; i < 2; i++) { XmlElement xObject = xmlDoc.CreateElement("object"); if (xObject != null) { xn_Root.AppendChild(xObject); } CreateNode(xmlDoc, xObject, "name", result.Targets[i].Label.ToLower()); CreateNode(xmlDoc, xObject, "pose", "Unspecified"); CreateNode(xmlDoc, xObject, "truncated", "0"); CreateNode(xmlDoc, xObject, "difficult", "0"); XmlElement xBndbox = xmlDoc.CreateElement("bndbox"); XmlNode xn_objext = xmlDoc.SelectSingleNode("annotation").SelectNodes("object")[i]; xn_objext.AppendChild(xBndbox); CreateNode(xmlDoc, xBndbox, "xmin", Math.Round(result.Targets[i].Box.X - result.Targets[i].Box.Width / 2).ToString()); CreateNode(xmlDoc, xBndbox, "ymin", Math.Round(result.Targets[i].Box.Y - result.Targets[i].Box.Height / 2).ToString()); CreateNode(xmlDoc, xBndbox, "xmax", Math.Round(result.Targets[i].Box.X + result.Targets[i].Box.Width / 2).ToString()); CreateNode(xmlDoc, xBndbox, "ymax", Math.Round(result.Targets[i].Box.Y + result.Targets[i].Box.Height / 2).ToString()); } try { xmlDoc.Save(path + "\" + filename + ".xml"); } catch (Exception e) { } } /// <summary> /// 创建节点 /// </summary> /// <param name="xmlDoc"></param> /// <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);
https://blog.csdn.net/weixin_44338429/article/details/88378741