/*************************************
* XmlNodeReader *
* ***********************************
* A fast, non validating, in-line *
* XML Node reader *
* *
* Author: Tony POTTIER *
* Contact: contact@tonypottier.info *
* CODE IS COPYLEFT, USE AT YOUR *
* OWN RISKS. *
*************************************/
public enum XmlNodeType
{
Opening,
Closing
}
[Serializable()]
public class XmlNodeReader
{
private Dictionary<string, string> _attributes;
private string _line;
private XmlNodeType _xmlNodeType;
private string _name;
private string _innerText;
/// <summary>
/// The XML node as its string representation
/// </summary>
public string FullText
{
get
{
return _line;
}
}
/// <summary>
/// XML node Inner Text (empty if there is none)
/// </summary>
public string InnerText
{
get
{
return _innerText;
}
}
/// <summary>
/// XML Node Name
/// </summary>
public string Name
{
get
{
return _name;
}
}
/// <summary>
/// Opening or Closing tag.
/// Tags ending with "/>" are considered as Opening tags.
/// </summary>
public XmlNodeType XMLNodeType
{
get
{
return _xmlNodeType;
}
}
/// <summary>
/// The list of attributes of the XML Node
/// </summary>
public Dictionary<string, string> Attributes
{
get
{
return _attributes;
}
}
/// <summary>
/// Get an attribute value of the XML node
/// </summary>
/// <param name="attribute">The name of the attribute</param>
/// <returns>Empty string if attribute does not exist</returns>
public string getAttribute(string attribute)
{
if (_attributes.ContainsKey(attribute))
{
return _attributes[attribute];
}
else
{
return "";
}
}
public XmlNodeReader()
{
_attributes = new Dictionary<string, string>();
}
public XmlNodeReader(string line)
{
_line = line;
_attributes = new Dictionary<string, string>();
decodeLine();
}
public void decodeString(string line)
{
_line = line;
decodeLine();
}
private void decodeLine()
{
_attributes.Clear();
_name = "";
_innerText = "";
bool tagBegun = false;
bool searchAttributeName = false;
bool searchAttributeValue = false;
bool AttributeValueBegunFound = false;
//we're looking for the tag name before
//attributes
bool searchTagName = true;
string currentAttributeName = "";
string currentAttributeValue = "";
_xmlNodeType = XmlNodeType.Opening;
int i;
for (i = 0; i < _line.Length; i++)
{
if (tagBegun)
{
//test end
if (_line[i] == '>')
{ break; }
//search for the tag name before
//searching for attributes
if (searchTagName)
{
if (_line[i] == '/')
{
_xmlNodeType = XmlNodeType.Closing;
}
else if (_line[i] == ' ' && _name != "")
{
searchTagName = false;
}
else
{
_name += _line[i];
}
}
else
{
//find a new attribute
if (!searchAttributeValue && !searchAttributeName)
{
if (_line[i] != ' ')
{
searchAttributeName = true;
currentAttributeName += _line[i];
}
}
else if (searchAttributeName && !searchAttributeValue)
{
//look for '=' wich is the end of attribute name
if (_line[i] == '=')
{
searchAttributeName = false;
searchAttributeValue = true;
_attributes.Add(currentAttributeName.Trim(), "");
}
else
{
currentAttributeName += _line[i];
}
}
else if (!searchAttributeName && searchAttributeValue)
{
if (AttributeValueBegunFound)
{
if (_line[i] == '"')
{
//END OF VALUE PARSING
AttributeValueBegunFound = false;
searchAttributeValue = false;
searchAttributeName = false;
_attributes[currentAttributeName.Trim()] = currentAttributeValue;
currentAttributeName = "";
currentAttributeValue = "";
}
else
{
currentAttributeValue += _line[i];
}
}
else
{
//try to find the first " to start value parsing
if (_line[i] == '"')
{ AttributeValueBegunFound = true; }
}
}
}
}
else if (_line[i] == '<')
{ tagBegun = true; }
}
//now decode inner text if it exists
if (_line.Length + 1 > i)
{
for (i = i + 1; i < _line.Length; i++)
{
if (_line[i] == '<')
{ break; }
else
{
_innerText += _line[i];
}
}
}
}
}