我有一个用VB.NET编写的表单,用于将用户数据写入XML文件。每次使用表单时,都会生成一个新的XML文件,其中包含通过表单提供的数据。我的问题是我试图让XML输出类似于:
<PersonalData>
<products>
<product>
<productID>1</productID>
<productNumber>123456</productNumber>
</product>
</products>
<customers>
<customer>
<LastName>Winchester</LastName>
<FirstName>Sam</FirstName>
<Address>1234 Elm RD</Address>
<City>San Antonio</City>
<State>Texas</State>
<ZipCode>76345</ZipCode>
</customer>
</customers>
</PersonalData>
然而,我最终得到了:
<PersonalData>
<products>
<product>
<productID>1</productID>
<productNumber>123456</productNumber>
<customers>
<customer>
<LastName>Winchester</LastName>
<FirstName>Sam</FirstName>
<Address>1234 Elm RD</Address>
<City>San Antonio</City>
<State>Texas</State>
<ZipCode>76345</ZipCode>
</customer>
</customers>
</product>
</products>
</PersonalData>
这是我的代码 - 按钮点击 - :
Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim textWriter As New XmlTextWriter(Server.MapPath("xml/PersonDataVI.xml"), Nothing)
textWriter.Formatting = System.Xml.Formatting.Indented
'Start New Document
textWriter.WriteStartDocument()
'Write a Comment
textWriter.WriteComment("This is a comment")
'Insert Start Element -root element node
textWriter.WriteStartElement("PersonalData")
'write the start element
textWriter.WriteStartElement("products")
'write the child start element
textWriter.WriteStartElement("product")
'Write ProductID Element and Data
textWriter.WriteStartElement("productID", "")
textWriter.WriteString(txtProductID.Text)
textWriter.WriteEndElement()
'Write ProductID Element and Data
textWriter.WriteStartElement("productNumber", "")
textWriter.WriteString(txtProductNum.Text)
textWriter.WriteEndElement()
'write the child element
textWriter.WriteStartElement("customers")
'write the child element
textWriter.WriteStartElement("customer")
'Write LastName Element and Data
textWriter.WriteStartElement("LastName", "")
textWriter.WriteString(txtLastName.Text)
textWriter.WriteEndElement()
'Write FirstName Element and Data
textWriter.WriteStartElement("FirstName", "")
textWriter.WriteString(txtFirstName.Text)
textWriter.WriteEndElement()
'Write Address Element and Data
textWriter.WriteStartElement("Address", "")
textWriter.WriteString(txtAddress.Text)
textWriter.WriteEndElement()
'Write City Element and Data
textWriter.WriteStartElement("City", "")
textWriter.WriteString(txtCity.Text)
textWriter.WriteEndElement()
'Write State Element and Data
textWriter.WriteStartElement("State", "")
textWriter.WriteString(txtState.Text)
textWriter.WriteEndElement()
'Write ZipCode Elment and Data
textWriter.WriteStartElement("ZipCode", "")
textWriter.WriteString(txtZipCode.Text)
textWriter.WriteEndElement()
'End Everything
textWriter.WriteEndDocument()
'Clean up
textWriter.Flush()
textWriter.Close()
'Display the XML Document
Response.Redirect(Server.MapPath("xml/PersonDataVI.xml"))
End Sub
我在这里做错了什么?
答案 0 :(得分:2)
您错过了WriteEndElement
和product
的{{1}}。
在
products
你还需要两个
textWriter.WriteStartElement("productNumber", "")
textWriter.WriteString(txtProductNum.Text)
textWriter.WriteEndElement()
答案 1 :(得分:0)
在为产品编号执行end元素之后,您需要再对end元素执行两次调用。
Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim textWriter As New XmlTextWriter(Server.MapPath("xml/PersonDataVI.xml"), Nothing)
textWriter.Formatting = System.Xml.Formatting.Indented
'Start New Document
textWriter.WriteStartDocument()
'Write a Comment
textWriter.WriteComment("This is a comment")
'Insert Start Element -root element node
textWriter.WriteStartElement("PersonalData")
'write the start element
textWriter.WriteStartElement("products")
'write the child start element
textWriter.WriteStartElement("product")
'Write ProductID Element and Data
textWriter.WriteStartElement("productID", "")
textWriter.WriteString(txtProductID.Text)
textWriter.WriteEndElement()
'Write ProductID Element and Data
textWriter.WriteStartElement("productNumber", "")
textWriter.WriteString(txtProductNum.Text)
textWriter.WriteEndElement()
textWriter.WriteEndElement()
textWriter.WriteEndElement()
'write the child element
textWriter.WriteStartElement("customers")
'write the child element
textWriter.WriteStartElement("customer")
'Write LastName Element and Data
textWriter.WriteStartElement("LastName", "")
textWriter.WriteString(txtLastName.Text)
textWriter.WriteEndElement()
'Write FirstName Element and Data
textWriter.WriteStartElement("FirstName", "")
textWriter.WriteString(txtFirstName.Text)
textWriter.WriteEndElement()
'Write Address Element and Data
textWriter.WriteStartElement("Address", "")
textWriter.WriteString(txtAddress.Text)
textWriter.WriteEndElement()
'Write City Element and Data
textWriter.WriteStartElement("City", "")
textWriter.WriteString(txtCity.Text)
textWriter.WriteEndElement()
'Write State Element and Data
textWriter.WriteStartElement("State", "")
textWriter.WriteString(txtState.Text)
textWriter.WriteEndElement()
'Write ZipCode Elment and Data
textWriter.WriteStartElement("ZipCode", "")
textWriter.WriteString(txtZipCode.Text)
textWriter.WriteEndElement()
'End Everything
textWriter.WriteEndDocument()
'Clean up
textWriter.Flush()
textWriter.Close()
'Display the XML Document
Response.Redirect(Server.MapPath("xml/PersonDataVI.xml"))
End Sub
答案 2 :(得分:0)
如果您使用的是VB 9或更高版本,请考虑使用LINQ to XML而不是显式编写器。有了这个,你可以做到:
Dim myData = <PersonalData>
<products>
<product>
<productID>1</productID>
<productNumber>123456</productNumber>
</product>
</products>
<customers>
<customer>
<LastName>Winchester</LastName>
<FirstName>Sam</FirstName>
<Address>1234 Elm RD</Address>
<City>San Antonio</City>
<State>Texas</State>
<ZipCode>76345</ZipCode>
</customer>
</customers>
</PersonalData>
要插入值,只需像在ASP.Net中那样转义它们:
<productID><% myProduct.ID %></productID>