0

I am looking for workaround to write only "modified objects" to file without overwriting existing nodes/elements. I know how to Serialize object to XML using XmlSerializer class, but this involves re-writing entire XML.

Also, How is it possible with ConfigurationSection, ConfigurationElement classes of System.Configuration namespace?

So far, I have tried below code:

    // <summary>This method serializes the contents of the current object to the specified file</summary>
    /// <param name="fileName">The file name</param>
    /// <returns>True if the serialize was successful</returns>
    private bool SerializeObjectToFile(string fileName)
    {
        bool retVal = false;

        try
        {
            using (Stream fStream =
                new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //'UserDataObject ' is the class which I want to serialize
                (new XmlSerializer(typeof(UserDataObject))).Serialize(fStream, this);

                retVal = true;
            }
        }
        catch (Exception ex)
        {
            // Error
            //LogObj.Logger.WriteTrace("Exception=" + ex.Message);
        }

        return retVal;
    }

sample XML format as below:

<?xml version="1.0"?>
<UserDataObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <ExistingItems>
        <LocalObject>
            <Name>FirstObject</Name>
            :
        </LocalObject>
        :
     </ExistingItems>

     <NewItems>
        <MyNewObject>
            <User>Admin</User>
            <LastConnected>9/6/2017 4:00PM</LastConnected>
            :
        </MyNewObject>
        :
     </NewItems>
    </UserDataObject>

Does anyone knows how to insert/update only particular <section> or <element> of existing XML?

Thanks!

Ketan
  • 79
  • 1
  • 9
  • 2
    Xml are text files and I doubt you can do this. Nor can I believe it would really make any difference performance-wise. – TaW Jun 09 '17 at 17:02
  • okay. if XML is quite large, wouldn't that cause a performance concern? Still, I am wondering how is it possible with ConfigurationSection, ConfigurationElement of ".config" file to write and modify
    or ?
    – Ketan Jun 12 '17 at 04:27
  • Shall I derive my object from `CustomSettings` class and assign 'ConfigurationProperty' attributes to properties? This should enable editing sections in config. – Ketan Jun 12 '17 at 06:21
  • I can't advise on which structure to use, but unless the files are huge (>100MB) performance shouldn't be a problem you could help with. Beyond that either partition the data or consider a DBMS. The only text files one can speed up are [fixed length records](https://stackoverflow.com/questions/39315287/optimize-large-dictionary-for-reading-in-net/39319898#39319898), which is not what you can create with xml. – TaW Jun 12 '17 at 07:19
  • 1
    @TaW is right. You cannot insert anything into the middle of a file without re-writing the remainder. It's just not possible to do anything else (normally). In XML the ownership of an item depends solely on its position in the file... As TaW says, putting the data into a DBMS temporarily would assist with making a lot of changes followed by a single re-write of the entire file. Another option is to put the file on a RAM filesystem; that'll be very fast then, even if you are re-writing the file several times over. However, filesystem caching by the OS might not be much slower than this... – bazza Jun 12 '17 at 20:14

1 Answers1

0

I managed to implement it with below class, which is available in .NET. If anyone has better way to do it.. would be interested to know.:)

How to: Create Custom Configuration Sections Using ConfigurationSection

Ketan
  • 79
  • 1
  • 9