public List <MenuModel> ConfigParse(string s) { XMLParser xp = new XMLParser(); XMLNode xn = xp.Parse(s); var list = new List <MenuModel>(); int no = 0; while (no < 300) { var menuParent = new MenuModel(); menuParent.menuTitle = xn.GetValue(string.Format("Menu>0>ParentMenu>{0}>@menuTitle", no)); menuParent.iconName = xn.GetValue(string.Format("Menu>0>ParentMenu>{0}>@iconName", no)); if (menuParent.menuTitle.Contains("标签越界") || menuParent.menuTitle.Contains("标签不存在") || menuParent.iconName.Contains("标签越界") || menuParent.iconName.Contains("标签不存在")) { break; } int subNo = 0; while (subNo < 300) { var menuChild = new MenuModel(); menuChild.menuTitle = xn.GetValue(string.Format("Menu>0>ParentMenu>{0}>childMenu>{1}>@menuTitle", no, subNo)); menuChild.iconName = xn.GetValue(string.Format("Menu>0>ParentMenu>{0}>childMenu>{1}>@iconName", no, subNo)); menuChild.url = xn.GetValue(string.Format("Menu>0>ParentMenu>{0}>childMenu>{1}>@url", no, subNo)); if (menuChild.menuTitle.Contains("标签越界") || menuChild.menuTitle.Contains("标签不存在") || menuChild.iconName.Contains("标签越界") || menuChild.iconName.Contains("标签不存在") || menuChild.url.Contains("标签越界") || menuChild.url.Contains("标签不存在")) { break; } menuParent.subList.Add(menuChild); subNo++; } list.Add(menuParent); no++; } return(list); }
public List <ConfigModel> ConfigParse(string s) { XMLParser xp = new XMLParser(); XMLNode xn = xp.Parse(s); List <ConfigModel> cms = new List <ConfigModel>(); int no = 0; while (no < 300) { ConfigModel cm = new ConfigModel(); cm.sourcePath = xn.GetValue(string.Format("Config>0>ConfigModel>{0}>@SourcePath", no)); if (cm.sourcePath.Contains("标签越界") || cm.sourcePath.Contains("标签不存在")) { break; } cm.clientFileName = xn.GetValue(string.Format("Config>0>ConfigModel>{0}>@ClientFileName", no)); cm.clientDirectory = xn.GetValue(string.Format("Config>0>ConfigModel>{0}>@ClientDirectory", no)); cms.Add(cm); no++; } return(cms); }
public int Push(XMLNode item) { Add(item); return(this.Count); }
public XMLNode Parse(string content) { XMLNode rootNode = new XMLNode(); rootNode["_text"] = ""; string nodeContents = ""; bool inElement = false; bool collectNodeName = false; bool collectAttributeName = false; bool collectAttributeValue = false; bool quoted = false; string attName = ""; string attValue = ""; string nodeName = ""; string textValue = ""; bool inMetaTag = false; bool inComment = false; bool inCDATA = false; XMLNodeList parents = new XMLNodeList(); XMLNode currentNode = rootNode; for (int i = 0; i < content.Length; i++) { char c = content[i]; char cn = '~'; // unused char char cnn = '~'; // unused char char cp = '~'; // unused char if ((i + 1) < content.Length) { cn = content[i + 1]; } if ((i + 2) < content.Length) { cnn = content[i + 2]; } if (i > 0) { cp = content[i - 1]; } if (inMetaTag) { if (c == QMARK && cn == GT) { inMetaTag = false; i++; } continue; } else { if (!quoted && c == LT && cn == QMARK) { inMetaTag = true; continue; } } if (inComment) { if (cp == DASH && c == DASH && cn == GT) { inComment = false; i++; } continue; } else { if (!quoted && c == LT && cn == EXCLAMATION) { if (content.Length > i + 9 && content.Substring(i, 9) == "<![CDATA[") { inCDATA = true; i += 8; } else { inComment = true; } continue; } } if (inCDATA) { if (c == SQR && cn == SQR && cnn == GT) { inCDATA = false; i += 2; continue; } textValue += c; continue; } if (inElement) { if (collectNodeName) { if (c == SPACE) { collectNodeName = false; } else if (c == GT) { collectNodeName = false; inElement = false; } if (!collectNodeName && nodeName.Length > 0) { if (nodeName[0] == SLASH) { // close tag if (textValue.Length > 0) { currentNode["_text"] += textValue; } textValue = ""; nodeName = ""; currentNode = parents.Pop(); } else { if (textValue.Length > 0) { currentNode["_text"] += textValue; } textValue = ""; XMLNode newNode = new XMLNode(); newNode["_text"] = ""; newNode["_name"] = nodeName; if (currentNode[nodeName] == null) { currentNode[nodeName] = new XMLNodeList(); } XMLNodeList a = (XMLNodeList)currentNode[nodeName]; a.Push(newNode); parents.Push(currentNode); currentNode = newNode; nodeName = ""; } } else { nodeName += c; } } else { if (!quoted && c == SLASH && cn == GT) { inElement = false; collectAttributeName = false; collectAttributeValue = false; if (attName.Length > 0) { if (attValue.Length > 0) { currentNode["@" + attName] = attValue; } else { currentNode["@" + attName] = true; } } i++; currentNode = parents.Pop(); attName = ""; attValue = ""; } else if (!quoted && c == GT) { inElement = false; collectAttributeName = false; collectAttributeValue = false; if (attName.Length > 0) { currentNode["@" + attName] = attValue; } attName = ""; attValue = ""; } else { if (collectAttributeName) { if (c == SPACE || c == EQUALS) { collectAttributeName = false; collectAttributeValue = true; } else { attName += c; } } else if (collectAttributeValue) { if (c == QUOTE || c == QUOTE2) { if (quoted) { collectAttributeValue = false; currentNode["@" + attName] = attValue; attValue = ""; attName = ""; quoted = false; } else { quoted = true; } } else { if (quoted) { attValue += c; } else { if (c == SPACE) { collectAttributeValue = false; currentNode["@" + attName] = attValue; attValue = ""; attName = ""; } } } } else if (c == SPACE) { } else { collectAttributeName = true; attName = "" + c; attValue = ""; quoted = false; } } } } else { if (c == LT) { inElement = true; collectNodeName = true; } else { textValue += c; } } } return(rootNode); }
private object GetObject(string path) { string[] bits = path.Split('>'); XMLNode currentNode = this; XMLNodeList currentNodeList = null; bool listMode = false; object ob; for (int i = 0; i < bits.Length; i++) { if (listMode) { try { currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])]; ob = currentNode; listMode = false; } catch (Exception e) { return(bits[i - 1] + "#标签越界"); } } else { ob = currentNode[bits[i]]; if (ob == null) { if ((i + 1) == bits.Length) { return(bits[i] + "#属性不存在"); } else { return(bits[i] + "#标签不存在"); } } if (ob is ArrayList) { currentNodeList = (XMLNodeList)(ob as ArrayList); listMode = true; } else { // reached a leaf node/attribute if (i != (bits.Length - 1)) { // unexpected leaf node string actualPath = ""; for (int j = 0; j <= i; j++) { actualPath = actualPath + ">" + bits[j]; } //Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath); } return(ob); } } } if (listMode) { return(currentNodeList); } else { return(currentNode); } }