using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace JavaCode.cs { public class QGisHelper { public static void Create() { String path = @"E:\terrait\TianJin\ExportMap\ExportMap\Sources\"; String appendStr = " " + "\r\n " + "\r\n "; string testPath = Path.Combine(path, "Test.qpt"); string testXml = File.ReadAllText(testPath); String newTestXml = testXml.Replace("\"./", "\"" + path) .Replace(" ", appendStr); File.WriteAllText(testPath, newTestXml); appendStr = " " + "\r\n " + "\r\n "; String tempXml = testXml .Replace("printResolution=\"300\"", "printResolution=\"{dpi}\"") .Replace("labelText=\"管道基础大数据平台(DW1-1)\"", "labelText=\"{title}\"") .Replace("中国石油天然气管道工程有限公司", "中国 x x x x x 工程有限公司") .Replace("\"./Lengend.png\"", "\"{sourcesPath}\\Lengend.png\"") .Replace("\"./logo.png\"", "\"{sourcesPath}\\logo.png\"") .Replace("mapRotation=\"0\"", "mapRotation=\"{rotation}\"") .Replace(getStrByKey(testXml, "xmin"), "xmin=\"{xmin}") .Replace(getStrByKey(testXml, "ymin"), "ymin=\"{ymin}") .Replace(getStrByKey(testXml, "ymax"), "ymax=\"{ymax}") .Replace(getStrByKey(testXml, "xmax"), "xmax=\"{xmax}") .Replace("\"河北省\"", "\"{province}\"") .Replace("\"1:25万\"", "\"{scale}\"") .Replace("\"20米\"", "\"{resolution}\"") .Replace("\"2022.11.06\"", "\"{date}\"") .Replace(" ", appendStr); ; string tempPath = Path.Combine(path, "Template.qpt"); File.WriteAllText(tempPath, tempXml); MessageBox.Show("生成完毕!"); } private static String getStrByKey(string xml, string key) { int start = xml.IndexOf(key); int end = xml.IndexOf("\"", start + key.Length + 3); //string ss = ""; return xml.Substring(start, end - start); } } }