/// <summary> /// 打开文档 /// </summary> public void OpenDoc() { try { if (m_enDocMode == XMLState.xmlDocClosed) { if (File.Exists(m_cXMLFilename)) { m_objDoc = new XmlDocument(); m_objDoc.Load(m_cXMLFilename); m_objRoot = m_objDoc.DocumentElement; if (m_objRoot == null) { throw (new Exception("打开文档不正确!")); } m_enDocMode = XMLState.xmlDocOpen; } } else { MessageBox.Show("打开XML文档错误"); return; } } catch (Exception ex) { ErrorMsg("打开XML文档错误", ex.Message, ex.StackTrace, "OpenDoc"); } }
/// <summary> /// 开始执行 /// </summary> /// <param name="bIncludeLayerNames"></param> public void start(bool bIncludeLayerNames) { ReadLUT(); m_enDocMode = XMLState.xmlDocClosed; m_objDoc = new XmlDocument(); if (!string.IsNullOrEmpty(m_cXMLFilename)) { OpenDoc(); } }
private void ReadXML() { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Application.StartupPath; ofd.Filter = "XML Files(.xml)|*.xml"; if (DialogResult.OK == ofd.ShowDialog(this)) { XmlTextReader xtr = new XmlTextReader(ofd.FileName); XMLState state = new XMLState(); state = XMLState.Empty; while (xtr.Read()) { switch (xtr.NodeType) { case XmlNodeType.Element: if (xtr.Name == "Book") { state = XMLState.Book; } if (xtr.Name == "Imię") { switch (state) { case XMLState.Book: addBook[0] = xtr.ReadString(); if (xtr.ReadString() == null) { addBook[0] = ""; } break; } } } } } }
private void PrettifyXML(bool prettifyWhole) { XMLState state = XMLState.Start, lastState = XMLState.Start; WindowsAPI.BeginPaint(this); int cursor = 0; int currentPosition = SelectionStart; int startPos = 0; char quote = '\0'; int start, end; if (prettifyWhole) { start = 0; end = TextLength - 1; } else { if (lastPosition.Count > 1) { start = FindLast(Text, lastPosition[lastPosition.Count - 2] > currentPosition ? currentPosition - 1 : lastPosition[lastPosition.Count - 2], '<'); end = FindFirst(Text, currentPosition, '>'); } else { start = 0; end = TextLength - 1; } } for (int i = start; i <= end; i++) { cursor = i; if (state != XMLState.inString && state != XMLState.inComment) { if (Text[i] == '\"') { quote = '\"'; SetColor(cursor, 1, HunterConfig.ColorXMLString); lastState = state; state = XMLState.inString; continue; } else if (Text[i] == '\'') { quote = '\''; SetColor(cursor, 1, HunterConfig.ColorXMLString); lastState = state; state = XMLState.inString; continue; } } switch (state) { case XMLState.Start: if (Text[i] == '<') { state = XMLState.inElement; } else if (Text[i] == '&') { lastState = state; state = XMLState.inMaybeEscape; startPos = i; } SetColor(cursor, 1, HunterConfig.ColorXMLPlain); break; case XMLState.inString: if (Text[i] == quote) { //还原状态 state = lastState; quote = '\0'; } SetColor(cursor, 1, HunterConfig.ColorXMLString); break; case XMLState.inElement: if (Text[i] != '?' && Text[i] != '>' && !Char.IsWhiteSpace(Text[i]) && Text[i] != '!' && Text[i] != '/') { SetColor(cursor, 1, HunterConfig.ColorXMLElement); } else if (Text[i] == '?' || Text[i] == '/') { SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Text[i] == '>') { state = XMLState.Start; SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Text[i] == '!') { lastState = state; state = XMLState.inMaybeComment; startPos = i; SetColor(cursor, 2, HunterConfig.ColorXMLPlain); } else if (Char.IsWhiteSpace(Text[i])) { state = XMLState.inAttribute; startPos = i + 1; } break; case XMLState.inAttribute: if (Text[i] != '=' && !Char.IsWhiteSpace(Text[i]) && Text[i] != '>' && Text[i] != '?' && Text[i] != '<') { SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Text[i] == '=') { SetColor(startPos, cursor - startPos, HunterConfig.ColorXMLAttribute); SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Char.IsWhiteSpace(Text[i])) { startPos = i + 1; } else if (Text[i] == '>') { state = XMLState.Start; SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Text[i] == '<') { state = XMLState.inElement; SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } else if (Text[i] == '?') { SetColor(cursor, 1, HunterConfig.ColorXMLPlain); } break; case XMLState.inMaybeComment: if (Text[i] == '-' && i + 1 < TextLength && Text[i + 1] == '-') { state = XMLState.inComment; SetColor(startPos - 1, 4, HunterConfig.ColorXMLComment); i++; } else { state = lastState; } break; case XMLState.inComment: if (Text[i] == '-') { startPos = i; if (i + 2 < TextLength) { if (Text[i + 1] == '-' && Text[i + 2] == '>') { state = XMLState.Start; SetColor(cursor, 3, HunterConfig.ColorXMLComment); i += 2; } } } else { SetColor(cursor, 1, HunterConfig.ColorXMLComment); } break; case XMLState.inMaybeEscape: if (Text[i] == ';') { state = lastState; } SetColor(startPos, cursor - startPos + 1, HunterConfig.ColorXMLEscape); break; } } Select(currentPosition, 0); WindowsAPI.EndPaint(this); }
/// <summary> /// 新建一个XML文档 /// </summary> /// <param name="OverWrite">是否重写</param> /// <param name="blnIncludeLayerNames">是否包含图层名称</param> /// <returns></returns> public bool CreateNewFile(bool OverWrite, bool blnIncludeLayerNames) { XmlDeclaration objDeclare = default(XmlDeclaration); string cNamePre = ""; try { if (File.Exists(m_cXMLFilename)) { if (OverWrite == false) { throw (new Exception("当前XML文件已存在")); return(false); } File.Delete(m_cXMLFilename); } cNamePre = GetNamespacePrefix(m_sRootNodeName); m_objDoc = new XmlDocument(); m_objRoot = m_objDoc.CreateElement(cNamePre, GetOGCName(m_sRootNodeName), GetNamespaceURL(cNamePre)); m_objDoc.AppendChild(m_objRoot); m_objActiveNode = m_objRoot; if (blnIncludeLayerNames) { //标准SLD文件 CreateAttribute("version"); SetAttributeValue(m_cSLDVersion); } else { //世界地图SLD CreateAttribute("xmlns"); SetAttributeValue(m_SLDXmlns); } //写入XML命名空间 foreach (string key in m_objNamespaceURL.Keys) { CreateAttribute("xmlns" + ":" + key); SetAttributeValue(m_objNamespaceURL[key]); } objDeclare = m_objDoc.CreateXmlDeclaration(m_cXMLVersion, m_cXMLEncoding, "yes"); //XML版本和XML编码规则 m_objDoc.InsertBefore(objDeclare, m_objRoot); SaveDoc(); //保存新建的文档 m_enDocMode = XMLState.xmlDocOpen; //XML命名空间管理器 m_objNSManager = new XmlNamespaceManager(m_objDoc.NameTable); foreach (string key in m_objNamespaceURL.Keys) { m_objNSManager.AddNamespace(key, m_objNamespaceURL[key]); } return(true); } catch (Exception ex) { ErrorMsg("新建XML文档失败 (" + ex.Message + ")", ex.Message, ex.StackTrace, "CreateNewFile"); return(false); } }