// ------------------------------------------------------------------------------------------------------------- public void Save() { #if !UNITY_WEBPLAYER File.CreateText(mXmlFilename).Close(); EditorXmlNode document = EditorXmlNode.CreateDocument(); EditorXmlNode records = new EditorXmlNode(EditorXmlNodeType.ELEMENT, "records"); EditorXmlNode record = null; document.AttachChild(records); foreach (string key in mRecords.Keys) { record = new EditorXmlNode(EditorXmlNodeType.ELEMENT, "record"); record.SetAttribute("name", key); record.SetAttribute("value", mRecords[key]); records.AttachChild(record); } File.WriteAllText(mXmlFilename, document.ToXmlString(true)); #endif }
public static EditorXmlNode ParseDocument(string xml, EditorXmlNodeType ignore) { EditorXmlNode emptydocument = new EditorXmlNode(EditorXmlNodeType.DOCUMENT, "XmlDocument", ""); EditorXmlNode document = new EditorXmlNode(EditorXmlNodeType.DOCUMENT, "XmlDocument", ""); XmlRawData data = new XmlRawData(xml); EditorXmlNode current = document; EditorXmlNode node; string tagname = string.Empty; string content = string.Empty; int mark; int textmark = -1; while (data.valid) { if (data.actual == '<') { // Check if text was found before entering the tag if (textmark != -1) { content = data.GetData(textmark, data.pointer - textmark); if (content.Trim() == string.Empty) { if (!((ignore & EditorXmlNodeType.WHITESPACE) == EditorXmlNodeType.WHITESPACE)) { node = new EditorXmlNode(EditorXmlNodeType.WHITESPACE, "XmlWhitespace", content); current.AttachChild(node); } } else { if (!((ignore & EditorXmlNodeType.TEXT) == EditorXmlNodeType.TEXT)) { node = new EditorXmlNode(EditorXmlNodeType.TEXT, "XmlText", content); current.AttachChild(node); } } textmark = -1; } // Xml declaration ? if (data.next == '?') { data.Advance(2); mark = data.pointer; // Get the node's name while (data.valid && data.actual != '?' && !data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("The declaration was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); if (tagname == string.Empty) { //data.say ("An Xml declaration can not have an empty name"); return(emptydocument); } node = new EditorXmlNode(EditorXmlNodeType.DECLARATION, tagname); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } while (data.valid && data.actual != '?') { if (!data.IsOn(WHITESPACES)) { mark = data.pointer; while (data.valid && data.actual != '=') { data.Advance(); } if (!data.valid) { //data.say ("All attributes must be matched with a value"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); data.Advance(); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("All attributes must have a value specified"); return(emptydocument); } while (data.valid && data.actual != '"') { data.Advance(); } data.Advance(); mark = data.pointer; while (data.valid && data.actual != '"') { data.Advance(); } if (!data.valid) { //data.say ("All attribute's values must be enclosed with double quotes"); return(emptydocument); } content = data.GetData(mark, data.pointer - mark); node.SetAttribute(tagname, content); } data.Advance(); } if (!data.valid) { //data.say ("End of declaration was not closed properly"); return(emptydocument); } current.AttachChild(node); data.Advance(2); } // Comments and CDATA else if (data.next == '!') { // Comments ? if (data.GetData(4) == "<!--") { data.Advance(4); mark = data.pointer; while (data.valid && (data.actual != '-' || data.GetData(3) != "-->")) { data.Advance(); } if (!data.valid) { //data.say ("Comment was not closed properly"); return(emptydocument); } if (!((ignore & EditorXmlNodeType.COMMENT) == EditorXmlNodeType.COMMENT)) { node = new EditorXmlNode(EditorXmlNodeType.COMMENT, "XmlComment", data.GetData(mark, data.pointer - mark)); current.AttachChild(node); } data.Advance(2); } else { //data.say ("Tag type could not be determined"); return(emptydocument); } } // Closing element else if (data.next == '/') { data.Advance(2); mark = data.pointer; while (data.valid && data.actual != '>') { data.Advance(); } if (!data.valid) { //data.say ("End of element was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark); if (string.Compare(tagname, current.name) != 0) { //data.say ("Closing a not previously opened element (" + tagname + "," + current.name + ")"); return(emptydocument); } current = current.parent; } // Everything else (expecting a new element) else { data.Advance(); mark = data.pointer; // Get the node's name while (data.valid && data.actual != '>' && data.actual != '/' && !data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("The element was not closed properly"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); if (tagname == string.Empty) { //data.say ("A tag can not have an empty name"); return(emptydocument); } node = new EditorXmlNode(EditorXmlNodeType.ELEMENT, tagname); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } while (data.valid && data.actual != '>') { if (data.actual == '/' && data.GetData(2) == "/>") { break; } else if (data.actual == '>') { break; } else if (!data.IsOn(WHITESPACES)) { mark = data.pointer; while (data.valid && data.actual != '=') { data.Advance(); } if (!data.valid) { //data.say ("All attributes must be matched with a value"); return(emptydocument); } tagname = data.GetData(mark, data.pointer - mark).Trim(); data.Advance(); while (data.valid && data.IsOn(WHITESPACES)) { data.Advance(); } if (!data.valid) { //data.say ("All attributes must have a value specified"); return(emptydocument); } while (data.valid && data.actual != '"') { data.Advance(); } data.Advance(); mark = data.pointer; while (data.valid && data.actual != '"') { data.Advance(); } if (!data.valid) { //data.say ("All attribute's values must be enclosed with double quotes"); return(emptydocument); } content = data.GetData(mark, data.pointer - mark); node.SetAttribute(tagname, content); } data.Advance(); } if (!data.valid) { //data.say ("End of element was not closed properly"); return(emptydocument); } if (data.actual == '>') { current.AttachChild(node); current = node; } else if (data.actual == '/') { current.AttachChild(node); data.Advance(); } } } else { if (textmark == -1) { textmark = data.pointer; } } data.Advance(); } return(document); }