zoukankan      html  css  js  c++  java
  • 自动生成xml

      #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

  • 相关阅读:
    linux编程之main()函数启动过程【转】
    dlmalloc(一)【转】
    Linux进程调度原理【转】
    Linux内存管理原理【转】
    malloc原理和内存碎片【转】
    Linux MTD系统剖析【转】
    linux驱动开发:用户空间操作LCD显示简单的图片【转】
    LCD驱动分析【转】
    LCD常用接口原理【转】
    LCD之mipi DSI接口驱动调试流程【转】
  • 原文地址:https://www.cnblogs.com/Manuel/p/13473770.html
Copyright © 2011-2022 走看看