使用winforms创建自定义xml [英] Creating customized xml using winforms

查看:102
本文介绍了使用winforms创建自定义xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个按钮。当我单击第一个按钮时,将创建startdocument和其他内容,然后当我单击第二个按钮时,它必须附加内容并保存文件。但是,当我点击我得到一个错误。 无效的xml文档和文档需要一个根元素。



There are two buttons. When I click first button the startdocument and the other contents will be created and then when I click second button it has to append the content and save the file . But when I click i get an error . "Invalid xml document and document needs a root element.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace GenerateXmlVertwo
{
    public partial class Form1 : Form
    {

        static XmlDocument doc = new System.Xml.XmlDocument();
        XmlWriter writer = doc.CreateNavigator().AppendChild();

        public Form1()
        {
            InitializeComponent();

            writer.WriteStartDocument();
            writer.WriteStartElement("seleniumObjectRepository");
            writer.WriteStartElement("seleniumRepObjects");
            MessageBox.Show("Pritned start element");
        }


        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void button1_Click(object sender, EventArgs e)
        {
            //  System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            //   using (XmlWriter writer = doc.CreateNavigator().AppendChild())
            // {
            // Do this directly 
            //  XmlWriterSettings xws = new XmlWriterSettings();

            writer.WriteStartElement("seleniumRepObject");
            writer.WriteAttributeString("Class", "WebElement");
            writer.WriteAttributeString("Name", "RegisterLink");
            writer.WriteStartElement("seleniumRepProperty");
            writer.WriteAttributeString("Name", "xpath");
            writer.WriteElementString("seleniumRepValue", "registerWithoutClaim");

            MessageBox.Show("printed the content");

            // }



        }

        public void button1_Click_1(object sender, EventArgs e)
        {

            try
            {

                writer.WriteEndElement();
                writer.WriteEndDocument();
                doc.Save(@"filelocation\vas.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }

        }


    }
}

推荐答案

错误信息是可以理解的。请参阅xml文档:可扩展标记语言(XML)1.1 - 文档 [ ^ ]:

The error message is quite understandable. Please, refer xml documentation: Extensible Markup Language (XML) 1.1 - documents[^]:
Quote:

[定义: 只有一个元素,称为root,或者文档元素 ,其中没有任何部分出现在任何其他元素的内容中。]对于所有其他元素,如果开始标记位于另一个元素的内容中,则结束标记位于同一元素的内容。更简单地说,由开始和结束标记分隔的元素在彼此之间正确嵌套。

[Definition: There is exactly one element, called the root, or document element, no part of which appears in the content of any other element.] For all other elements, if the start-tag is in the content of another element, the end-tag is in the content of the same element. More simply stated, the elements, delimited by start- and end-tags, nest properly within each other.





所以,你需要添加根元素到你的xml:



So, you need to add root element to your xml:

<somenameofrootelement>
    <seleniumobjectrepository></seleniumobjectrepository>
    <seleniumrepobjects></seleniumrepobjects>
</somenameofrootelement>







我建议你改变你的逻辑...



我不建议你创建全局对象(即使它只在表单类中使用)。而不是它,在按钮单击事件中打开,编辑和保存xml文档。



请参阅:

XmlReader类 [ ^ ]

如何:使用XmlReader解析XML [ ^ ]

XmlWriter类 [ ^ ]

结合XmlReader和XmlWriter类进行简单的流转换 [ ^ ]



最终注意:

操作xml数据的最简单方法是使用 xml序列化和反序列化 [ ^ ]。




I'd suggest you to change your logic...

I do not recommend you to create global objects (even if it's used only within form class). Rather than it, open, edit and save xml document within button click event.

See:
XmlReader Class[^]
How to: Parse XML with XmlReader[^]
XmlWriter Class[^]
Combining the XmlReader and XmlWriter classes for simple streaming transformations[^]

Final note:
The simplest way to operate on xml data is to use xml serialization and deserialization[^].


这篇关于使用winforms创建自定义xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆