// adds new element(s) // several tags can be concattenated by dots // in that case as many subelements as tags are created public XMLElement AddChild(string TagName, string Content) { string[] parts = TagName.Split('.'); XMLElement currentParent = this; for (int i = 0; i < parts.Length - 1; ++i) { XMLElement existingChild = currentParent.FindByType(parts[i]); if (existingChild != null) { currentParent = existingChild; } else { XMLElement newChild = new XMLElement(parts[i]); currentParent.AddChild(newChild); currentParent = newChild; } } XMLElement newElement = new XMLElement(parts[parts.Length - 1], Content); currentParent.AddChild(newElement); return(newElement); }
internal override XMLElement ToXML() { XMLElement e = XMLElement.Create(GetType().Name); e.AddAttribute("FilterCount", $"{FilterCount}"); e.AddAttribute("FilterSize", $"{FilterSize}"); e.AddAttribute("Stride", $"{Stride}"); e.AddAttribute("ZeroPadding", $"{ZeroPadding}"); e.AddChild(Activation.ToXML()); StringBuilder sb = new StringBuilder(); XMLElement weightsXE = XMLElement.Create("Weights"); e.AddChild(weightsXE); for (int i = 0; i < this.weights.Length; i++) { if (i != 0) { sb.Append(","); } sb.Append(this.weights[i]); } weightsXE.Value = sb.ToString(); sb.Clear(); XMLElement biasesXE = XMLElement.Create("Biases"); e.AddChild(biasesXE); for (int i = 0; i < this.biases.Length; i++) { if (i != 0) { sb.Append(","); } sb.Append(this.biases[i]); } biasesXE.Value = sb.ToString(); return(e); }
public string ToXML() { XMLElement rootXE = XMLElement.Create(GetType().Name); rootXE.AddAttribute("Layers", this.layers.Length.ToString()); for (int i = 0; i < this.layers.Length; i++) { XMLElement layerXE = this.layers[i].ToXML(); layerXE.AddAttribute("Index", $"{i}"); rootXE.AddChild(layerXE); } return(XMLWriter.Parse(rootXE)); }
internal XMLElement ToXML() { XMLElement e = XMLElement.Create("ActivationFunction"); e.AddAttribute("Name", Name); XMLElement customDataXE = XMLElement.Create("CustomData"); e.AddChild(customDataXE); foreach (KeyValuePair <string, (Type t, object val)> tuple in this.customData) { XMLElement tupleXE = XMLElement.Create(tuple.Key); tupleXE.AddAttribute("Type", tuple.Value.t.FullName); tupleXE.AddAttribute("Value", tuple.Value.val.ToString()); customDataXE.AddChild(tupleXE); } return(e); }
private bool ParseTag(string xmlText, int iCurrentPos) { while (iCurrentPos < xmlText.Length) { string strTagContent = ""; m_iCurrentParsePos = iCurrentPos; // Tag-Anfang int iTagStartPos = xmlText.IndexOf('<', iCurrentPos); if (iTagStartPos == -1) { //System.out.println( "no tag start<" ); m_Error = "No tag start at " + iCurrentPos; return(false); } if (iTagStartPos > m_iCurrentParsePos) { strTagContent = xmlText.Substring(m_iCurrentParsePos, iTagStartPos - m_iCurrentParsePos); strTagContent = strTagContent.Trim(); } int iTagEndPos = xmlText.IndexOf('>', iTagStartPos); if (iTagEndPos - iTagStartPos < 2) { //System.out.println( "no tag end>" ); m_Error = "No tag end at " + iTagStartPos; return(false); } string tagContent = xmlText.Substring(iTagStartPos + 1, iTagEndPos - iTagStartPos - 1); string tagCurrent = ""; //System.out.println( "tagContent " + tagContent ); XMLElement currentTag = null; XMLElement curElement = new XMLElement(); if (m_listOpenTags.Count > 0) { currentTag = m_listOpenTags.Last.Value; } if (currentTag != null) { currentTag.Content = strTagContent; } if (tagContent.StartsWith("!--")) { if (!tagContent.EndsWith("--")) { m_Error = "Comment tag not ending as expected"; return(false); } curElement.Content = tagContent; m_Childs.Add(curElement); m_iCurrentParsePos = iTagEndPos + 1; iCurrentPos = m_iCurrentParsePos; continue; } if (tagContent.StartsWith("/")) { // Ende-Tag if (m_listOpenTags.Count == 0) { //System.out.println( "Closing tag but no more open tags" ); m_Error = "closing tag but no more open tags"; return(false); } //System.out.println( "closing tag: " + tagContent + " Open " + m_listOpenTags.size() ); tagContent = tagContent.Substring(1); if (tagContent != currentTag.Type) { //System.out.println( "closing tag mismatch " + tagContent + " != " + currentTag.getType() ); m_Error = "closing tag mismatch " + tagContent + " != " + currentTag.Type; return(false); } m_iCurrentParsePos = iTagEndPos + 1; return(true); } bool autoClosing = tagContent.EndsWith("/"); if (autoClosing) { tagContent = tagContent.Substring(0, tagContent.Length - 1); } int iSpacePos = tagContent.IndexOf(' '); if (iSpacePos == -1) { // nur das Element selbst, keine Attribute tagCurrent = tagContent; } else { tagCurrent = tagContent.Substring(0, iSpacePos); } if (!autoClosing) { m_listOpenTags.AddLast(curElement); //System.out.println( "=Tag Open (" + tagContent + ") = " + m_listOpenTags.size() ); } else { //tagContent = tagContent.substring( 0, tagCurrent.length() ); } curElement.Type = tagCurrent; if (currentTag != null) { //System.out.println( "Adding Tag " + tagCurrent + " as child of " + currentTag.getType() ); currentTag.AddChild(curElement); } //addChild( curElement ); if (iSpacePos != -1) { // Attribute parsen int iAttrPos = iSpacePos + 1; while (iAttrPos < tagContent.Length) { // Attribut-Ende suchen int iEqualPos = tagContent.IndexOf('=', iAttrPos); if (iEqualPos == -1) { //System.out.println( "attribut missing =" ); m_Error = "attribut missing ="; return(false); } // Attribut kann Leerzeichen enthalten!! string attributName = tagContent.Substring(iAttrPos, iEqualPos - iAttrPos).Trim(); string attribut = ""; int attributeEnd = -1; if (tagContent[iEqualPos + 1] == '\"') { int secondApostrophe = tagContent.IndexOf('\"', iEqualPos + 2); if (secondApostrophe == -1) { //System.out.println( "attribute missing second apostrophe" ); m_Error = "attribute missing second apostrophe"; return(false); } attributeEnd = secondApostrophe + 1; attribut = tagContent.Substring(iEqualPos + 1, secondApostrophe + 1 - iEqualPos - 1); } else { // Attribut-Ende beim nächsten Leerzeichen attributeEnd = tagContent.IndexOf(' ', iAttrPos); attribut = tagContent.Substring(iEqualPos + 1, attributeEnd - iEqualPos - 1); } if ((attribut.Length >= 2) && (attribut[0] == '"') && (attribut[attribut.Length - 1] == '"')) { attribut = attribut.Substring(1, attribut.Length - 2); } //System.out.println( "Checking attribute: " + attributName + " = " + attribut ); curElement.AddAttribute(attributName, attribut); iAttrPos = attributeEnd + 1; } } if (!ParseTag(xmlText, iTagEndPos + 1)) { //System.out.println( "Failed to parse child tag" ); //m_Error = "Failed to parse child tag"; return(false); } if (!autoClosing) { m_listOpenTags.RemoveLast(); //System.out.println( "=Tag Close (" + tagContent + ") = " + m_listOpenTags.size() ); } else { return(true); //System.out.println( "==Not autoclosing tag (" + tagContent + ") = " + m_listOpenTags.size() ); } iCurrentPos = m_iCurrentParsePos; } return(true); }