public static XmlDocument XMLOptimazerByType(XmlDocument XMLOriginDoc) { XmlDocument resDoc = new XmlDocument(); XmlNode tmpNode; XmlNode themeNode = null, resDocthemeNode, elementNode_created, viewNode_created; string elementNodeName = "", elementName = ""; //XmlNode propertiesNodeToBeOptimized = null; string nameofview = ""; bool elementNotExist; //List<string> worked = new List<string>(); //int findIndx = -1; SortedList <string, XmlNode> optimizedNodes = new SortedList <string, XmlNode>(); //получаем Theme node и удаляем ее из документа foreach (XmlNode currentNode in XMLOriginDoc) { if (currentNode.Name == "theme") { themeNode = currentNode; tmpNode = themeNode; XMLOriginDoc.RemoveChild(themeNode); } } //Create the main element for the theme resDocthemeNode = resDoc.CreateElement("theme"); resDoc.AppendChild(resDocthemeNode); resDocthemeNode.AppendChild(XMLWorker.createXMLNode(resDoc, "formatVersion", "", "", "4")); if (themeNode == null) { return(null); } //если уже есть объединенные view мы их переносим в результирующую ноду. for (int i = 0; i < themeNode.ChildNodes.Count; i++) { if (themeNode.ChildNodes[i].Name == "view" && themeNode.ChildNodes[i].Attributes["name"] != null && themeNode.ChildNodes[i].Attributes["name"].InnerText.Contains(",")) { XmlNode importNode = resDoc.ImportNode(themeNode.ChildNodes[i], true); resDocthemeNode.AppendChild(importNode); optimizedNodes.Add(themeNode.ChildNodes[i].Attributes["name"].InnerText, importNode); themeNode.RemoveChild(themeNode.ChildNodes[i]); i--; } } //проходимся по view элементы которых будем сравнивать. for (int i = 0; i < (int)Element.types.notexistsname; i++) { foreach (XmlNode viewNode in themeNode.ChildNodes) { //если имя Node view if (viewNode.Name == "view") { //проходимся по всем элемента текущего view. foreach (XmlNode elementNode in viewNode) { if ((((Element.types) 1).ToString()) == elementNode.Name) { //Нужно пройти по всем properties childNode(для нашей проги это Properties) и найти их копии foreach (XmlNode propertiesNode in elementNode) { nameofview = ""; elementNodeName = ""; foreach (XmlNode viewnodeWhereFind in themeNode.ChildNodes) { if (viewnodeWhereFind.Name == "view" && !viewNode.Equals(viewnodeWhereFind)) { foreach (XmlNode elementnodeWhereFind in viewnodeWhereFind) { if (elementNode.Name == elementnodeWhereFind.Name) { foreach (XmlNode propertiesNodeWhereFind in elementnodeWhereFind) { if (propertiesNodeWhereFind.Name == propertiesNode.Name) { if (nameofview == "") { nameofview = viewNode.Attributes["name"].InnerText; } if (viewnodeWhereFind.Attributes["name"].InnerText != null && !nameofview.Contains(viewnodeWhereFind.Attributes["name"].InnerText)) { nameofview += ", " + viewnodeWhereFind.Attributes["name"].InnerText; } elementNodeName = elementNode.Name; if (elementName == "") { elementName = elementNode.Attributes["name"].InnerText; } if (!elementName.Contains(elementnodeWhereFind.Attributes["name"].InnerText)) { elementName += ", " + elementnodeWhereFind.Attributes["name"].InnerText; } XMLWorker.createXmlAttribute(XMLOriginDoc, propertiesNodeWhereFind, "remove", "true"); } } } } } } //После того как прошли по всем нодам сохраняем elementNotExist = true; string sortednameofview = sortString(nameofview); if (sortednameofview != "")// && sortednameofview.Contains(", ") { if (optimizedNodes.IndexOfKey(sortednameofview) >= 0) { viewNode_created = optimizedNodes.Values[optimizedNodes.IndexOfKey(sortednameofview)]; foreach (XmlNode elementNodeCreated in viewNode_created) { if (elementNodeCreated.Name == elementNodeName) { elementNotExist = false; if (getIndexNode(elementNodeCreated, propertiesNode) < 0) { elementNodeCreated.AppendChild(XMLWorker.createXMLNode(resDoc, propertiesNode.Name, "", "", propertiesNode.InnerText)); XMLWorker.createXmlAttribute(XMLOriginDoc, propertiesNode, "remove", "true"); } } } if (elementNotExist) { elementNode_created = XMLWorker.createXMLNode(resDoc, elementNodeName, "name", elementName, ""); elementNode_created.AppendChild(XMLWorker.createXMLNode(resDoc, propertiesNode.Name, "", "", propertiesNode.InnerText)); viewNode_created.AppendChild(elementNode_created); XMLWorker.createXmlAttribute(XMLOriginDoc, propertiesNode, "remove", "true"); } } else { viewNode_created = XMLWorker.createXMLNode(resDoc, "view", "name", sortednameofview, ""); elementNode_created = XMLWorker.createXMLNode(resDoc, elementNodeName, "name", elementName, ""); elementNode_created.AppendChild(XMLWorker.createXMLNode(resDoc, propertiesNode.Name, "", "", propertiesNode.InnerText)); viewNode_created.AppendChild(elementNode_created); optimizedNodes.Add(sortednameofview, viewNode_created); XMLWorker.createXmlAttribute(XMLOriginDoc, propertiesNode, "remove", "true"); } //elementNode.RemoveChild(propertiesNode); } } } } } } } for (int i = 0; i < optimizedNodes.Count; i++) { //resDocthemeNode.RemoveChild(optimizedNodes.Values[i]); try { resDocthemeNode.AppendChild(optimizedNodes.Values[i]); } catch (Exception) { resDocthemeNode.AppendChild(resDoc.ImportNode(optimizedNodes.Values[i], true)); } } themeNode = clearXmlNode(themeNode); foreach (XmlNode viewNode in themeNode.ChildNodes) { resDocthemeNode.AppendChild(resDoc.ImportNode(viewNode, true)); } resDoc.AppendChild(resDocthemeNode); return(resDoc); }
private void generateXML() { if (_viewtmplatewindow != null && _viewtmplatewindow.isOpened) { _viewtmplatewindow.save(); } if (views.Values.Count > 0) { XmlNode docNode, viewNode, itemNode, subItemNode, themeNode, featureNode; string type, propval, alpha, color; //List<string> selectedview = new List<string>(); string nameOfElement; //Create xml document doc = new XmlDocument(); docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(docNode); //Create the main element for the theme themeNode = doc.CreateElement("theme"); doc.AppendChild(themeNode); themeNode.AppendChild(XMLWorker.createXMLNode(doc, "formatVersion", "", "", "4")); //selectedview.Clear(); for (int i = 0; i < views.Values.Count; i++) { //Create the main section of type view with the name selected in the ComboBox viewNode = XMLWorker.createXMLNode(doc, "view", "name", views.Values[i].name, ""); themeNode.AppendChild(viewNode); if (!string.IsNullOrEmpty(views.Values[i].background)) { itemNode = XMLWorker.createXMLNode(doc, "image", "name", "background", ""); if (views.Values[i].name == "system") { XMLWorker.createXmlAttribute(doc, itemNode, "extra", "true"); } else { XMLWorker.createXmlAttribute(doc, itemNode, "extra", "false"); } subItemNode = XMLWorker.createXMLNode(doc, "path", "", "", views.Values[i].background); itemNode.AppendChild(subItemNode); subItemNode = XMLWorker.createXMLNode(doc, "pos", "", "", "0.0 0.0"); itemNode.AppendChild(subItemNode); subItemNode = XMLWorker.createXMLNode(doc, "origin", "", "", "0.0 0.0"); itemNode.AppendChild(subItemNode); subItemNode = XMLWorker.createXMLNode(doc, "size", "", "", "1.0 1.0"); itemNode.AppendChild(subItemNode); viewNode.AppendChild(itemNode); } if (views.Values[i].name == "system") { if (!string.IsNullOrEmpty(views.Values[i].bgsound)) { itemNode = XMLWorker.createXMLNode(doc, "sound", "name", "bgsound", ""); subItemNode = XMLWorker.createXMLNode(doc, "path", "", "", views.Values[i].bgsound); itemNode.AppendChild(subItemNode); viewNode.AppendChild(itemNode); } if (!string.IsNullOrEmpty(views.Values[i].logo)) { itemNode = XMLWorker.createXMLNode(doc, "image", "name", "logo", ""); subItemNode = XMLWorker.createXMLNode(doc, "path", "", "", views.Values[i].logo); itemNode.AppendChild(subItemNode); viewNode.AppendChild(itemNode); } } for (int j = 0; j < views.Values[i].elements.Count; j++) { //selectedview.Add(views.Values[i].elements.Values[j].name); type = views.Values[i].elements.Values[j].typeOfElement.ToString(); itemNode = XMLWorker.createXMLNode(doc, type, "name", views.Values[i].elements.Values[j].name, ""); if (views.Values[i].elements.Values[j].extra != "") { XMLWorker.createXmlAttribute(doc, itemNode, "extra", views.Values[i].elements.Values[j].extra.ToString().ToLower()); } if (views.Values[i].elements.Values[j].name.ToLower() == "logo" && views.Values[i].elements.Values[j].path == "") { continue; } else { for (int x = 0; x < views.Values[i].elements.Values[j].Properties.Count; x++) { propval = ""; alpha = ""; color = ""; propval = views.Values[i].elements.Values[j].Properties.Values[x]; if (views.Values[i].elements.Values[j].Properties.Keys[x].ToLower().Contains("color")) { //переносим альфа канал в конец строки цвета propval = propval.Replace("#", ""); if (propval.Length == 8) { alpha = propval.Substring(0, 2); color = propval.Substring(2, propval.Length - 2); propval = color + alpha; } } if (views.Values[i].elements.Values[j].Properties.Keys[x].ToLower().Contains("text") && propval == "") { continue; } subItemNode = XMLWorker.createXMLNode(doc, views.Values[i].elements.Values[j].Properties.Keys[x], "", "", propval); itemNode.AppendChild(subItemNode); } } viewNode.AppendChild(itemNode); } if (!views.Values[i].name.Contains(",")) { for (int y = 0; y < views.Values[i].novisibleelement.Count; y++) { nameOfElement = views.Values[i].novisibleelement[y]; //for basic and system view hide only help view. if ((views.Values[i].name == View.types.basic.ToString() && views.Values[i].novisibleelement[y] != "help") || (views.Values[i].name == View.types.system.ToString() && views.Values[i].novisibleelement[y] != "help")) { break; } if (views.Values[i].elements.IndexOfKey(nameOfElement) < 0) { itemNode = XMLWorker.createXMLNode(doc, (Element.GetType(nameOfElement)).ToString(), "name", nameOfElement, ""); subItemNode = XMLWorker.createXMLNode(doc, "pos ", "", "", "1.0 1.0"); itemNode.AppendChild(subItemNode); viewNode.AppendChild(itemNode); } } } } var stringBuilder = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; doc.Save(XmlWriter.Create(stringBuilder, xmlWriterSettings)); tb_log.Text = stringBuilder.ToString(); } }