private void AddInsertsToChangeLog(List <OptimizedChangeLogElement> insertsGmlIds, string wfsUrl,
                                           XElement changeLog, int datasetId)
        {
            List <string> typeNames = new List <string>();

            ChangelogWFS wfs = new ChangelogWFS();

            Dictionary <string, string> typeIdDict = new Dictionary <string, string>();

            List <string> gmlIds = new List <string>();

            long handle = 0;

            foreach (OptimizedChangeLogElement insert in insertsGmlIds)
            {
                gmlIds.Add(insert.GmlId);

                if (insert.Handle > handle)
                {
                    handle = insert.Handle;
                }
            }

            XElement getFeatureResponse = wfs.GetFeatureCollectionFromWFS(_pNsPrefixTargetNamespace, _pNsApp, wfsUrl,
                                                                          ref typeIdDict, gmlIds, datasetId);

            //Build inserts for each typename
            XNamespace nsWfs    = "http://www.opengis.net/wfs/2.0";
            XNamespace nsChlogf = "http://skjema.geonorge.no/standard/geosynkronisering/1.1/endringslogg";

            XNamespace nsApp = _pNsApp;
            // 20130917-Leg: Fix
            string nsPrefixApp      = changeLog.GetPrefixOfNamespace(nsApp);
            XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());

            mgr.AddNamespace(nsPrefixApp, nsApp.NamespaceName);
            string nsPrefixAppComplete = nsPrefixApp + ":";

            XElement insertElement = new XElement(nsWfs + "Insert", new XAttribute("handle", handle));

            foreach (KeyValuePair <string, string> dictElement in typeIdDict)
            {
                XElement feature = FetchFeatureByLokalid(dictElement.Key, getFeatureResponse);
                if (feature == null)
                {
                    throw new XmlException(dictElement.Value + " with localId " + dictElement.Key + " could not be found by WFS-server");
                }
                insertElement.Add(feature);
                AddReferencedFeatureToChangelog(insertElement, feature, getFeatureResponse);
            }

            changeLog.Element(nsChlogf + "transactions").Add(insertElement);
        }
        private void AddReplaceToChangeLog(string gmlId, long handle, string wfsUrl, XElement changeLog, int datasetId)
        {
            Dictionary <string, string> typeIdDict = new Dictionary <string, string>();
            ChangelogWFS wfs = new ChangelogWFS();
            XElement     getFeatureResponse = wfs.GetFeatureCollectionFromWFS(_pNsPrefixTargetNamespace, _pNsApp, wfsUrl,
                                                                              ref typeIdDict, new List <string>()
            {
                gmlId
            }, datasetId);
            XNamespace nsWfs    = "http://www.opengis.net/wfs/2.0";
            XNamespace nsChlogf = "http://skjema.geonorge.no/standard/geosynkronisering/1.1/endringslogg";
            XNamespace nsFes    = "http://www.opengis.net/fes/2.0";

            XNamespace nsApp = _pNsApp;
            // 20130917-Leg: Fix
            string nsPrefixApp                  = changeLog.GetPrefixOfNamespace(nsApp);
            string nsPrefixAppComplete          = nsPrefixApp + ":";
            string xpathExpressionLokalidFilter = nsPrefixAppComplete + "identifikasjon/" + nsPrefixAppComplete +
                                                  "Identifikasjon/" + nsPrefixAppComplete + "lokalId";

            foreach (KeyValuePair <string, string> dictElement in typeIdDict)
            {
                XElement feature        = FetchFeatureByLokalid(dictElement.Key, getFeatureResponse);
                XElement replaceElement = new XElement(nsWfs + "Replace", new XAttribute("handle", handle));
                replaceElement.Add(feature);

                //TODO: Check if this can be neccessary
                //AddReferencedFeatureToChangelog(replaceElement, feature, getFeatureResponse);
                replaceElement.Add(new XElement(nsFes + "Filter",
                                                new XElement(nsFes + "PropertyIsEqualTo",
                                                             new XElement(nsFes + "ValueReference", xpathExpressionLokalidFilter),
                                                             new XElement(nsFes + "Literal", dictElement.Key)
                                                             )
                                                ));
                changeLog.Element(nsChlogf + "transactions").Add(replaceElement);
            }
        }