Mr·GE 2023-07-13 09:43 采纳率: 51.5%
浏览 21
已结题

关于XML的格式问题

根据客户的需求,我做出来的是这样的

img


但实际客户想要的是这样的,怎么才能做出来这样的,标红的部分是区别

img

        private void button4_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.Rows.Count;
            string para1="";
            try
            {
                XmlDocument pXmlDocument = new XmlDocument();
                XmlDeclaration pXmlDeclaration = pXmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
                pXmlDocument.AppendChild(pXmlDeclaration);
                //加入一个根元素
                XmlElement pElement = pXmlDocument.CreateElement("", "root", "");
                pXmlDocument.AppendChild(pElement);

                XmlNode pXmlRootNode = pXmlDocument.SelectSingleNode("root");//拿到根元素

                //添加要素
                XmlElement pXmlRootElement = pXmlDocument.CreateElement("Title");
                XmlElement pXmlElement = pXmlDocument.CreateElement("DataModelCode");
                pXmlElement.InnerText = "DMXXXX";
                XmlElement pXmlElement1 = pXmlDocument.CreateElement("Version");
                pXmlElement1.InnerText = "1.00";

                XmlElement pXmlRootElement1 = pXmlDocument.CreateElement("MeasureContent");
                XmlElement pXmlElement2 = pXmlDocument.CreateElement("MeasuringItem");
                for (int i = 0; i < n-1; i++)
                {
                    para1 = para1 + "<item Code=\"" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "\" " +
                                    "Name=\"" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "\" " +
                                    "Value=\"" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "\" />" + "\r\n";
                }

                para1 = "<Item Code=\"JOBNAME\" Name=\"生产指令号\" Value=\"\">" + "\r\n" +
                        "<Item Code=\"SYSB\" Name=\"试验设备\" Value=\"\">" + "\r\n" + para1;

                pXmlElement2.InnerText = para1;

                pXmlRootElement.AppendChild(pXmlElement);
                pXmlRootElement.AppendChild(pXmlElement1);

                pXmlRootElement1.AppendChild(pXmlElement2);

                pXmlRootNode.AppendChild(pXmlRootElement);
                pXmlRootNode.AppendChild(pXmlRootElement1);
                pXmlDocument.Save("C:\\Users\\Administrator\\Desktop\\##测试程序\\datagridview操作\\test\\bin\\XML.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

  • 写回答

3条回答 默认 最新

  • MinX_Yu 2023-07-14 17:04
    关注

    使用

    <![CDATA[content]]> 
    
    

    还有 innerXml 可以保留内容,但是保存 文件后需要用文本替换将外面的包装去掉

    para1 = "<![CDATA[<hello >]]>";
    pXmlElement2.InnerXml = para1;
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月25日
  • 已采纳回答 7月17日
  • 创建了问题 7月13日

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡