Results 1 to 6 of 6

Thread: Having some trouble with XML Files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    18

    Having some trouble with XML Files

    Hey so I'm testing out using XML file to control a lot of settings for this app i'm creating. I've never worked with this before and I'm a little confused.

    If you leave Params blank it errors out but this is my code:

    Save Button
    Code:
            Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)
            'Check if the XML File Exists - Create if does not
            If IO.File.Exists(appData & "\ATDLauncherConfig.xml") = False Then
                Dim settings As New XmlWriterSettings()
                settings.Indent = True
                Dim XmlWrt As XmlWriter = XmlWriter.Create(appData & "\ATDLauncherConfig.xml", settings)
                With XmlWrt
                    .WriteStartDocument()
                    .WriteComment("ATDConfig")
                    .WriteStartElement("Settings")
                    .WriteStartElement("Arma2Path")
                    .WriteString(Arma2Path.Text)
                    .WriteEndElement()
                    .WriteStartElement("Arma2oaPath")
                    .WriteString(Arma2OAPath.Text)
                    .WriteEndElement()
                    .WriteStartElement("Params")
                    .WriteString(txtParams.Text)
                    .WriteEndElement()
                    .WriteEndElement()
                    .WriteEndDocument()
                    .Close()
                End With
                FlatAlertBox1.Visible = True
            Else
                'if it does exist - just update the entries
                Dim MyXML As New XmlDocument()
                MyXML.Load(appData & "\ATDLauncherConfig.xml")
                Dim Arma2PathNode As XmlNode = MyXML.SelectSingleNode("/Settings/Arma2Path")
                Arma2PathNode.ChildNodes(0).InnerText = "" & Arma2Path.Text
                Dim Arma2OAPathNode As XmlNode = MyXML.SelectSingleNode("/Settings/Arma2oaPath")
                Arma2OAPathNode.ChildNodes(0).InnerText = "" & Arma2OAPath.Text
                Dim Param As XmlNode = MyXML.SelectSingleNode("/Settings/Params")
                If txtParams.Text = "" Then txtParams.Text = vbNullString
                Param.ChildNodes(0).InnerText = txtParams.Text
                MyXML.Save(appData & "\ATDLauncherConfig.xml")
                FlatAlertBox1.Visible = True
            End If

    On Form Load:
    Code:
    Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)
            If IO.File.Exists(appData & "\ATDLauncherConfig.xml") = True Then
                Dim MyXML As New XmlDocument()
                MyXML.Load(appData & "\ATDLauncherConfig.xml")
                Dim Arma2PathNode As XmlNode = MyXML.SelectSingleNode("/Settings/Arma2Path")
                Arma2Path.Text = Arma2PathNode.ChildNodes(0).InnerText
                Dim Arma2OAPathNode As XmlNode = MyXML.SelectSingleNode("/Settings/Arma2oaPath")
                Arma2OAPath.Text = Arma2OAPathNode.ChildNodes(0).InnerText
                Dim Additional As XmlNode = MyXML.SelectSingleNode("/Settings/Params")
                If Not Additional.InnerText = "" Then
                    txtParams.Text = Additional.ChildNodes(0).InnerText
                End If
            End If
    Here is the XML File:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!--ATDConfig-->
    <Settings>
      <Arma2Path>test1</Arma2Path>
      <Arma2oaPath>test2</Arma2oaPath>
      <Params></Params>
    </Settings>
    This is what errors out:
    Code:
     Param.ChildNodes(0).InnerText = txtParams.Text



    Basically this is what happens:
    If the XML file exists and the value of <Params> is nothing then it throws errors on load or on save.
    If I manually edit the XML and put a value it in loads up just fine.
    If a value already exists in the XML file and I update it - it works fine.
    If the value exists and I make it blank - it saves just fine. I then reload the App and attempt to save an entry and it throws the same error again.

    Any ideas to why this happens in this way?

    Thanks guys!
    Attached Images Attached Images  
    Last edited by patnyc; Oct 20th, 2014 at 08:02 AM.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Having some trouble with XML Files

    You're setting param to be the Params node here:-
    Dim Param As XmlNode = MyXML.SelectSingleNode("/Settings/Params")

    You then issue this:-
    Param.ChildNodes(0).InnerText = txtParams.Text
    That will find the first child node of Param and set its inner text... except param doesn't have any child nodes. It just has a text value (which is it's inner text property).

    I think you probably just want:-
    Param.InnerText = txtParams.Text
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    18

    Re: Having some trouble with XML Files

    Thank you! - i have one word.

    Doh.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Having some trouble with XML Files

    Hello,

    Here is an alternate method to a) write data b) read data

    Code:
    Dim SomeVar1 As String = "test1"
    Dim SomeVar2 As String = "test2"
    
    Dim SettingsData =
        <AppSettings>
            <!--ATDConfig-->
            <Settings>
                <Arma2Path><%= SomeVar1 %></Arma2Path>
                <Arma2oaPath><%= SomeVar2 %></Arma2oaPath>
                <Params></Params>
            </Settings>
        </AppSettings>
    
    Dim Doc As XDocument =
        <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <%= SettingsData %>
    
    
    Doc.Save(
        IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "MySettings.xml"))
    
    ' ----------
    ' Read back data in another var
    ' ----------
    
    Dim Doc1 As New XDocument
    Doc1 = XDocument.Load(IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "MySettings.xml"))
    
    Dim Data =
        (
            From T In Doc1...<Settings>
            Let ParamValue = T.<Params>.Value
            Select New With
                   {
                       .S1 = T.<Arma2oaPath>.Value,
                       .S2 = T.<Arma2Path>.Value,
                       .S3 = If(String.IsNullOrWhiteSpace(ParamValue), "Empty", ParamValue)
                   }
               ).FirstOrDefault
    
    If Data IsNot Nothing Then
        Console.WriteLine("[{0}] [{1}] [{2}]", Data.S1, Data.S2, Data.S3)
    
    End If

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Having some trouble with XML Files

    Kev's suggestion is a good one (I personally find the xDocument syntax a little freindlier than the XmlDocument syntax) but be aware that if you use it you'll need to bring in System.Xml.Linq rather than System.Xml.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    18

    Re: Having some trouble with XML Files

    wow thats a great example! thanks! +rep for both

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width