0
Dim filter As String
filter = 'Bal_ln_id = 110 and Bal_ln_id = 100'     
Dim xmldoc As New System.Xml.XmlDocument
xmldoc.LoadXml(filter)

我只是想在 VB.net 中将字符串转换为 xml 文件。我得到一个 xmlException'Data at the root level is invalid. Line 1, position 1.'

我错过了什么吗?

我期待输出为

<DocumentElement>
  <DATA_TABLE>
    <BAL_LN_ID>110</BAL_LN_ID>
  </DATA_TABLE>
  <DATA_TABLE>
     <BAL_LN_ID>100</BAL_LN_ID>
  </DATA_TABLE>
</DocumentElement>'
4

1 回答 1

7

要使用XmlTextWriter该类执行此操作,您可以执行以下操作:

Private Function GenerateXml(ByVal ids As List(Of String)) As String
    Dim stringWriter As New StringWriter()
    Dim xmlWriter As New XmlTextWriter(stringWriter)
    xmlWriter.WriteStartDocument()
    xmlWriter.WriteStartElement("DocumentElement")
    For Each id As String In ids
        xmlWriter.WriteStartElement("DATA_TABLE")
        xmlWriter.WriteStartElement("BAL_LN_ID")
        xmlWriter.WriteString(id)
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndElement()
    Next
    xmlWriter.WriteEndElement()
    xmlWriter.WriteEndDocument()
    Return stringWriter.ToString()
End Function

然后你可以像这样使用它:

Dim ids As New List(Of String)()
ids.Add("110")
ids.Add("100")
Dim xml As String = GenerateXml(ids)
于 2012-12-10T18:26:24.440 回答