XmlWriter 不在 VB.net 中创建新元素 [英] XmlWriter Not Creating New Element in VB.net

查看:20
本文介绍了XmlWriter 不在 VB.net 中创建新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.net 写出一个 XML 文件.当我尝试创建另一个要写在第一个元素之后的元素时,它会出错说:

I'm writing out an XML file using VB.net. When I try to create another element to be written past the first, it errors out saying:

处于 EndRootElement 状态的令牌 StartElement 将导致无效的 XML 文档.如果要编写 XML 片段,请确保将 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 ConformanceLevel.Auto."

"Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment."

考虑到前一个元素已关闭,我不确定为什么要这样做.我试图寻找一个 writer.WriteEndRootElement,但我没有在那里看到任何东西.有什么建议可以让它工作吗?=)

I'm not sure why it is doing this considering that the previous element has been closed. I tried to look for a writer.WriteEndRootElement, but I didn't see any in there. Any suggestions to get it to work? =)

    Private Sub writeXMLFile(ByVal childform As Fone_Builder_Delux.frmData, ByVal filename As String)

        Dim xmlSettings As New XmlWriterSettings()
        xmlSettings.Indent = True
        xmlSettings.NewLineOnAttributes = True

        Using writer As XmlWriter = XmlWriter.Create(filename, xmlSettings)

            writer.WriteStartDocument()
            writer.WriteStartElement("header")

            writer.WriteStartAttribute("filepath")
            writer.WriteValue(filename)
            writer.WriteEndAttribute()

            writer.WriteEndElement()
            writer.WriteStartElement("variable")
            writer.WriteStartAttribute("varName")

            writer.WriteValue(childform.datagridHeaders.Item(0, 1))

            writer.WriteEndAttribute()
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Flush()

        End Using


    End Sub

推荐答案

一个 XML 文档只能有一个根元素.您正在启动文档,编写header"元素,关闭header"元素,然后启动一个新的variable"元素——这将是第二个根元素.

An XML document can have only one root element. You are starting the document, writing the "header" element, closing the "header" element, then starting a new "variable" element -- which would be a second root element.

要么将标题"和变量"都包含在一个更高级别的元素中,要么将其中一个移到另一个元素中.

Either enclose both "header" and "variable" within a single higher-level element, or move one of them inside the other.

这篇关于XmlWriter 不在 VB.net 中创建新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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