public string getCData(AgentPath path) { string cData = ""; // get us jump-started to the root element, putting us past any script noise typically in the beginning of the document; // note that if the root element is not the first of its type, then we can't just jump down like this int startOfBody = path.ElementIndex == 1 ? ResponseString.LastIndexOf("<" + path.ElementType) : 0; int index = path.positionOf(ResponseString, startOfBody); char[] docChars = ResponseString.ToCharArray(); bool flag = true; while (flag) { while (docChars[index] != '<') { cData += docChars[index++]; } // skip past comments if there are any if (docChars[index + 1] == '!' && docChars[index + 2] == '-' && docChars[index + 3] == '-') { while (!(docChars[index] == '>' && docChars[index - 1] == '-' && docChars[index - 2] == '-')) { index++; } index++; } else { flag = false; } } return(cData); }
public int positionOf(string document, int index) { Stack stack = new Stack(); char[] docChars = document.ToCharArray(); while (true) { // go until either an element starts, or an element ends while (docChars[index] != '<') { index++; } index++; // if an elements starts, check if it's a sibling if (docChars[index] != '/') { string elementType = ""; while (docChars[index] != ' ' && docChars[index] != '>') { elementType += docChars[index++]; } elementType = elementType.ToLower(); if (!elementType.StartsWith("!--")) { // zip past the element's end, stopping if for some reason a new element is discovered while (docChars[index] != '>' && docChars[index] != '<') { index++; } if (docChars[index] == '>') { index++; } // check if this is our guy - make sure the stack count is zero, since we only want to consider siblings if (stack.Count == 0 && elementType.Equals(ElementType)) { ElementIndex--; if (ElementIndex == 0) { // recursively call to our child, if we have one if (Next == null) { return(index); } else { return(Next.positionOf(document, index)); } } } // add to the stack stack.Push(elementType); } } // if an element ends, pop off of the stack else { index++; string elementType = ""; int backingUpIndex = index; // go to end of tag while (docChars[backingUpIndex] != '>') { backingUpIndex++; } // back up and get the closing element name while (docChars[--backingUpIndex] != '/') { elementType = docChars[backingUpIndex] + elementType; } elementType = elementType.ToLower(); // pop off until either the stack is empty, or our start is found string popped = ""; while (stack.Count > 0 && !popped.Equals(elementType)) { popped = (string)stack.Pop(); } } } }
public string getValue(AgentPath path, string attribute) { // get us jump-started to the root element, putting us past any script noise typically in the beginning of the document; // note that if the root element is not the first of its type, then we can't just jump down like this int startOfBody = path.ElementIndex == 1 ? ResponseString.LastIndexOf("<" + path.ElementType) : 0; int index = path.positionOf(ResponseString, startOfBody) - 2; // go behind the ending tag char[] docChars = ResponseString.ToCharArray(); // go backwards, and read off key/value pairs - if we reach the start tag, then the key wasn't found while (true) { // get data bool inQuotes = false; bool foundEquals = false; string elmValue = ""; while (!foundEquals) { if (docChars[index] == '\"') { inQuotes = !inQuotes; } else if (docChars[index] == '\'') { inQuotes = !inQuotes; } else if (docChars[index] == '=' && !inQuotes) { foundEquals = true; } else if (docChars[index] == '<' && !inQuotes) { throw new Exception("attribute " + attribute + " not found."); } else { elmValue = docChars[index] + elmValue; } index--; } while (docChars[index] == ' ') { index--; } string key = ""; while (docChars[index] != ' ') { key = docChars[index--] + key; } key = key.ToLower(); if (key.Equals(attribute)) { return(elmValue); } } }