添加、删除、修改 XML 个节点,而不 re-writting/overwritting 个带有可序列化对象的整个 XML 文件

Add, Remove, Modify XML nodes without re-writting/overwritting entire XML file with Serializable objects

我正在寻找仅将 "modified objects" 写入文件而不覆盖现有 nodes/elements 的解决方法。我知道如何使用 XmlSerializer class Serialize 反对 XML ,但这涉及重写整个 XML.

此外,ConfigurationSectionConfigurationElement classes System.Configuration 命名空间怎么可能?

到目前为止,我已经尝试过以下代码:

    // <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;
    }

样本XML格式如下:

<?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>

有谁知道如何 insert/update 现有 XML 的特定 <section><element>

谢谢!

我设法用以下 class 实现了它,它在 .NET 中可用。 如果有人有更好的方法来做到这一点..会有兴趣知道。:)

How to: Create Custom Configuration Sections Using ConfigurationSection