Adding Xml Fields into Newly Created NotePad

StuartK
StuartK used Ask the Experts™
on
Hi,

We have createa a new file uisng this code below
We want to populate it with about three fields using Xml

We use this code, but it doesn't work.

Dim strLocalPath As String = "C:\"
    Dim strConfigFileName As String = "Master.config"
If Not System.IO.File.Exists(strLocalPath & strConfigFileName) Then
            System.IO.File.Create(strLocalPath & strConfigFileName)

            Dim xd As New Xml.XmlDocument()
            xd.Load(strLocalPath & strConfigFileName)

            Dim NewInstalled As Xml.XmlNode
            NewInstalled = xd.CreateNode(Xml.XmlNodeType.Attribute, "Installed", Nothing)
            NewInstalled.InnerText = "ABCD"

but it doesn't work. Can anyone provide suitable code to create a new InnerText within this newly created .config file!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
Commented:
I think you are missing to save the xml document.  Please try the following.  If that does not work, please post the complete code.

Dim strLocalPath As String = "C:\"
Dim strConfigFileName As String = "Master.config"
If Not System.IO.File.Exists(strLocalPath & strConfigFileName) Then
            ' System.IO.File.Create(strLocalPath & strConfigFileName)

            Dim xd As New Xml.XmlDocument()
            ' xd.Load(strLocalPath & strConfigFileName)

            Dim NewInstalled As Xml.XmlNode
            NewInstalled = xd.CreateNode(Xml.XmlNodeType.Attribute, "Installed", Nothing)
            NewInstalled.InnerText = "ABCD"

            xd.Save(strLocalPath & strConfigFileName)
End If

Open in new window

Most Valuable Expert 2012
Top Expert 2014
Commented:
Commented:
Your code and question does not state what the new node name is, I have taken it on myselfto name it NodeName, so here goes:
        Dim xd As XDocument = Nothing
        Dim strLocalPath As String = "C:\"
        Dim strConfigFileName As String = "Master.config"
        If Not System.IO.File.Exists(strLocalPath & strConfigFileName) Then
            xd = New XDocument()
        Else
            xd = XDocument.Load(strLocalPath & strConfigFileName)
        End If
        Dim xNew As XElement = New XElement("NodeName", "ABCD", New XAttribute("Installed", Nothing))
        xd.Add(xNew)
        xd.Save(strLocalPath & strConfigFileName)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial