README.md

December 4, 2022 ยท View on GitHub

Create simple a Xml file in .Net

Description

Create your XML file by using the included XML classes in Visual Studio.

More Info

Submitted On
ByTimo Boehme
LevelBeginner
User Rating4.7 (14 globes from 3 users)
CompatibilityC#, VB.NET, ASP.NET
CategoryDatabases
World.Net (C#, VB.net)
Archive File

Source Code

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")