public XmlAttribute getAttribute(String strKey, String strDefaultReturn) { List<XmlAttribute> attributes = new List<XmlAttribute>(); foreach (XmlNode node in children) { if (node is XmlAttribute && ((XmlAttribute)node).strKey.Equals(strKey)) return (XmlAttribute)node; } XmlAttribute defaultReturn = new XmlAttribute(); defaultReturn.strKey = strKey; defaultReturn.strValue = strDefaultReturn; return defaultReturn; }
public static XmlElement loadXml(StreamReader streamReader, Action<String> logError) { XmlElement rootElement = null; XmlElement currentElement = null; XmlElement newElement = null; XmlAttribute currentAttribute = null; String strBlock = ""; var currentMode = Modes.TEXT; Boolean bIgnoreChar = false; Char cIgnoreChar = ' '; int iCurrent = 0; char c = '0'; while (streamReader.Peek() >= 0) { c = (char)streamReader.Read(); if (bIgnoreChar) { if (c == cIgnoreChar) { bIgnoreChar = false; strBlock = ""; } // else go on reading .... } else { switch (currentMode) { case Modes.ATTRIBUTE_KEY: { switch (c) { case '?': // xml definition { currentMode = Modes.TEXT; bIgnoreChar = true; cIgnoreChar = '>'; break; } case '/': // break element without text { currentElement = currentElement.parentNode; currentMode = Modes.TEXT; bIgnoreChar = true; cIgnoreChar = '>'; break; } case '=': { currentAttribute = new XmlAttribute(); currentAttribute.strKey = strBlock; // logConsole((iCurrent) + " " + currentElement.ElementName + ".addAttribute(" + currentAttribute.strKey + ")"); currentElement.addNode(currentAttribute); strBlock = ""; // goto value string bIgnoreChar = true; cIgnoreChar = '"'; currentMode = Modes.ATTRIBUTE_VALUE; break; } default: { strBlock += c; break; } } break; } case Modes.ATTRIBUTE_VALUE: { switch (c) { case '"': { currentAttribute.strValue = strBlock; strBlock = ""; currentMode = Modes.ELEMENT; // atribute done break; } default: { strBlock += c; break; } } break; } case Modes.ELEMENT: { switch (c) { case '?': { break; } // ignore question mark case ' ': { if (strBlock.Trim().Length > 0) { newElement = new XmlElement(currentElement, strBlock.Trim()); if (currentElement != null) { currentElement.addNode(newElement); // logConsole((iCurrent) + " " + currentElement.ElementName+".addElement("+newElement.ElementName+")"); } else rootElement = newElement; currentElement = newElement; strBlock = ""; } currentMode = Modes.ATTRIBUTE_KEY; break; } case '>': { if (strBlock.Trim().Length > 0) { newElement = new XmlElement(currentElement, strBlock.Trim()); if (currentElement != null) { currentElement.addNode(newElement); // logConsole((iCurrent) + " " + currentElement.ElementName + ".addElement(" + newElement.ElementName + ")"); } else rootElement = newElement; currentElement = newElement; strBlock = ""; } currentMode = Modes.TEXT; break; } case '/': { currentElement = currentElement.parentNode; currentMode = Modes.TEXT; bIgnoreChar = true; cIgnoreChar = '>'; break; } default: { strBlock += c; break; } } break; } default: { switch (c) { case '<': { if (currentElement != null) { if (strBlock.Trim().Length > 0) { currentElement.addNode(new XmlText(strBlock)); } // logConsole((iCurrent) + " " + currentElement.ElementName + ".addText("+strBlock+")"); } strBlock = ""; currentMode = Modes.ELEMENT; break; } default: { strBlock += c; break; } } break; } } } iCurrent++; } return rootElement; }