CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: XML Writer

  1. #1
    Join Date
    Apr 2009
    Location
    Bahamas
    Posts
    33

    Question XML Writer

    Hi, I have a settings file in my application that saves a few things. I created a XML reader to read the document when the program starts.

    I am now trying to figure out how to write the settings back to the file when the program shuts down.

    I'm not sure how to do it so I haven't been able to write any code yet. The problem I'm having is I want to replace the existing settings with new settings and I do not want to write over the rest of the file. There are many settings in here for my user interface. I've been looking on MSDN and MS stated that it does not automatically check for existing settings or duplicate nodes.

    Here is a sample of my setting file

    Code:
    <Settings>
    <Window-height>557<Window-height>
    <Window-width>413<Window-width>
    <Window-maximized>FALSE<Window-maximized>
    <Sequence>358911<Sequence>
    <Settings>
    This file only stored UI settings. When I close the form I want to save data into height and width and replace that existing data not create new nodes. I also want to ignore the secquence node or any other nodes because I don't need to save this data now but I don't want it overitten either.

    When loading I used the XMLDocument.SelectSingleNode but it appears I cannot use that for saving.

    Does anyone know the correct code to do this?
    Thanks,
    - pgrammer

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: XML Writer

    Hi.

    I think using the XML DOM through the XmlDocument and related classes would be best for you. Here's a simple example:
    Code:
    XmlDocument xdoc = new XmlDocument();
    // Load the file.
    xdoc.Load(<file location here>);
    
    // Select the nodes you want to change.
    XmlNode xnode = xdoc.SelectSingleNode("/Settings/Window-height");
    if (xnode != null)
    {
       // Change the inner text property
       xnode.InnerText = <new height here>;
    }
    // Now save the changes
    xdoc.Save(<file location here>);
    This code is not tested so you might have some issues with it but it gives you the general idea. Also you may need to try InnerXml if InnerText doesn't work. I normally use attributes for content so I'm not so familiar with those but I think InnerText is the property you want to change.

  3. #3
    Join Date
    Apr 2009
    Location
    Bahamas
    Posts
    33

    Re: XML Writer

    Oh, I tried something but I couldn't get the syntax right. I think I tried xmlWriter and xmlTextWriter but they wouldn't let me search the file and make changes.

    I will try this version. Thanks.
    Thanks,
    - pgrammer

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

Featured