示例#1
0
        //
        // DB WRITE into DATA SECTION replacing that section _______________________________________________
        //
        //Replace all Data between tags eg. WriteDATA(<DB_xy>,"Test", "this is new content"); <Test>this is new content</Test>
        void WriteDATA(string DB, string ident, string write)
        {
            // example string starts = "<dir_root>"; & string ends = "</dir_root>";
            string starts = "<" + ident + ">"; string ends = "</" + ident + ">";
            string DBname = ""; string DATA; //prep DB vars

            // GET fresh Data
            if (DB == "<DB_Me>")      // Default
            {
                DATA = Me.CustomData; // GetDATA by default to Me.CustomData
                // DBname = "Me";  Echo("DB read4write:" + DBname + ident);
            }// Only for feedback reasons
            else
            { // if not <DB_Me> get a Block and access its DB from CustomData
                DBname = DBlocate(DB);
                var Database = GridTerminalSystem.GetBlockWithName(DBname) as IMyTerminalBlock;
                if (Database != null)
                {
                    DATA = Database.CustomData;
                }                                                     // alternative if success
                else
                {
                    DATA = "Database not found: " + DBname;
                }
                // Echo("DB read4write:" + DBname + ident);
            }

            // Calculate Substring DB Part1
            int startPos = DATA.LastIndexOf(starts) + starts.Length;

            if (!(startPos > -1))
            {
                Echo("NoData found, thus no startPos. ERROR EXIT"); return;
            }                                                                                     // return empty if not found
            string DATA1 = DATA.Substring(0, startPos);
            // Calculate Substring DB Part3
            int totalLength = DATA.Length;
            int startPos3   = DATA.LastIndexOf(ends);

            if (!(startPos3 > -1))
            {
                Echo("NoData found, thus no startPos3. ERROR EXIT"); return;
            }                                                                                       // return empty if not found
            int    length3 = totalLength - startPos3;
            string DATA3   = DATA.Substring(startPos3, length3);
            // Adding Part 1, new Part 2 and Part 3
            string NewDATA = DATA1 + write + DATA3;

            //Echo("Old:" + DATA.Length + " NewPart:" + write.Length + " New:" + NewDATA.Length); //Debug

            // Writing Data
            if (DB == "<DB_Me>")         // Default get fresh Data
            {
                Me.CustomData = NewDATA; // WriteDATA by default to Me.CustomData
                // DBname = "Me"; Echo("DB write:" + DBname);
            }// Only for feedback reasons
            else
            { // if not <DB_Me> get a Block and access its DB from CustomData
                DBname = DBlocate(DB);
                var Database = GridTerminalSystem.GetBlockWithName(DBname) as IMyTerminalBlock;
                if (Database != null)
                {
                    Database.CustomData = NewDATA;
                }                                                        // alternative if success
                else
                {
                    Echo("No Database found!:" + DBname);
                }
                // Echo("DB write:" + DBname + ident);
            } // only feedback
        }