Erstellt eine XML Datei mit der XML Klasse aus VB.Net.
Ergebnis:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
<Parent AttributName="AttributWert">
<FirstElement>This is the text from the first element</FirstElement>
<SecondElement>This is the text from the second element</SecondElement>
<ThirdElement>This is the text from the third element</ThirdElement>
</Parent>
</RootNode>
Dim XmlDoc As New XmlDocument
'Write down the XML declaration
Dim XmlDeclaration As XmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
'Create the root element
Dim RootNode As XmlElement = XmlDoc.CreateElement("RootNode")
XmlDoc.InsertBefore(XmlDeclaration, XmlDoc.DocumentElement)
XmlDoc.AppendChild(RootNode)
'Create a new <Category> element and add it to the root node
Dim ParentNode As XmlElement = XmlDoc.CreateElement("Parent")
'Set attribute name and value!
ParentNode.SetAttribute("AttributName", "AttributWert")
XmlDoc.DocumentElement.PrependChild(ParentNode)
'Create the required nodes
Dim FirstElement As XmlElement = XmlDoc.CreateElement("FirstElement")
Dim SecondElement As XmlElement = XmlDoc.CreateElement("SecondElement")
Dim ThirdElement As XmlElement = XmlDoc.CreateElement("ThirdElement")
'retrieve the text
Dim FirstTextElement As XmlText = XmlDoc.CreateTextNode("This is the text from the first element")
Dim SecondTextElement As XmlText = XmlDoc.CreateTextNode("This is the text from the second element")
Dim ThirdTextElement As XmlText = XmlDoc.CreateTextNode("This is the text from the third element")
'append the nodes to the parentNode without the value
ParentNode.AppendChild(FirstElement)
ParentNode.AppendChild(SecondElement)
ParentNode.AppendChild(ThirdElement)
'save the value of the fields into the nodes
FirstElement.AppendChild(FirstTextElement)
SecondElement.AppendChild(SecondTextElement)
ThirdElement.AppendChild(ThirdTextElement)
'Save to the XML file
XmlDoc.Save("demo.xml")
Kommentare zum Snippet