I want to create XML files from scratch in Visual studio using VB.net
I found online Example but I struggled to understand how to add in some cases attributes and other cases child element . plus my top element have other elements.I mean my format is mucher longer but it looks like the below example:
-<CompanyFile>
-<Companybranch name="something">
-<Customer>
<name></name>
<age></age>
<address>
<addreesLine > </addreesLine>
<address>
</Customer>
</Companybranch>
</CompanyFile>
I found a link that has basic XML format.
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement("Table")
createNode(1, "Product 1", "1000", writer)
createNode(2, "Product 2", "2000", writer)
createNode(3, "Product 3", "3000", writer)
createNode(4, "Product 4", "4000", writer)
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End Sub
Private Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter)
writer.WriteStartElement("Product")
writer.WriteStartElement("Product_id")
writer.WriteString(pID)
writer.WriteEndElement()
writer.WriteStartElement("Product_name")
writer.WriteString(pName)
writer.WriteEndElement()
writer.WriteStartElement("Product_price")
writer.WriteString(pPrice)
writer.WriteEndElement()
writer.WriteEndElement()
End Sub
End Class
How can I create what I want One Node has List of elements then the list of elements may or may not have children or attributes.?
THANK YOU!
You can use feature that only vb.net language have - XML Literals with embed expressions.
Dim companyBranchName As String = "My Company"
Dim customerName As String = "Some customer Ltd"
Dim customerAge As Integer = 33
Dim addressLine As String = "Line 12"
Dim document As XDocument = <?xml version="1.0" encoding="UTF-8"?>
<CompanyFile>
<Companybranch name=<%= companyBranchName %>>
<Customer>
<name><%= customerName %></name>
<age><%= customerAge %></age>
<address>
<addreesLine><%= addressLine %></addreesLine>
<address>
</Customer>
</Companybranch>
</CompanyFile>
document.Save("path-to-the-file.xml)
Another approach is using serialization. XmlSerializer Class
Create classes which represent your data structures
Public Class CompanyFile
Public Property CompanyBranch As CompanyBranch
End Class
Public Class CompanyBranch
<XmlAttribute("name")> ' use attributes for explicitly declaring serialization logic
Public Property Name As String
Public Property Customer As Customer
End Class
Public Class Customer
Public Property Name As String
Public Property Age As Integer
Public Property Address As Address
End Class
Public Class Address
Public Property AddressLine As String
End Class
Then serialize data to the file
Dim data = New CompanyFile With
{
.CompanyBranch = New CompanyBranch With
{
.Name = "something",
.Customer = New Customer With
{
.Name = "customer name",
.Age = 33,
.Address = New Address With
{
.AddressLine = "Line 33"
}
}
}
}
Dim serializer = New XmlSerializer(GetType(CompanyFile))
Using writer As New StreamWriter("path-to-file.xml")
serializer.Serialize(writer, data)
End Using
Or se LINQ to Xml as mentioned in other answer
Dim xmlDeclaration As New XDeclaration("1.0", "UTF-8", "yes")
Dim document As XDocument = _
New XDocument(xmlDeclaration,
new XElement("CompanyFile",
new XElement("CompanyBranch",
new XAttrbute("name", companyName),
new XElement("Customer", "..."))));
document.Save("path-to-the-file.xml)
In case you going to save very big amount of data, then you can use your original approach with XmlWriter
, it less readable and more difficult to maintain, but very efficient with big amount of data, with XmlWriter
you don't need to have whole data in memory - you can write data in smaller chunks.
Dim settings As New XmlWriterSettings With
{
settings.Indent = True
}
Using writer As XmlWriter = XmlWriter.Create("path-to-file.xml", settings)
writer.WriteStartElement("CompanyFile")
' other elements
writer.WriteEndElement()
End Using
I would use Xml Linq which is a newer Net library with better features then the older Xml Net library.
See code below :
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim companyName As String = "Acme"
Dim customerName As String = "John"
Dim age As Integer = 2525
Dim address As String = "123 Main St."
Dim companyFile As XElement = New XElement("CompanyFile",
New XElement("Companybranch", New Object() {
New XAttribute("name", companyName),
New XElement("Customer", New Object() { _
New XElement("name", customerName),
New XElement("age", age),
New XElement("address",
New XElement("addressLine", address))
})
})
)
End Sub
End Module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With