Advertisement
Ansjh

SimpleXMLReader.cs

Aug 28th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SeriousRanker
  7. {
  8.   public class XMLTag
  9.   {
  10.     public string Name = "";
  11.     public Dictionary<string, string> Attributes = new Dictionary<string, string>();
  12.     public string Content = "";
  13.  
  14.     public string this[string key]
  15.     {
  16.       get
  17.       {
  18.         if (Attributes.ContainsKey(key))
  19.           return Attributes[key];
  20.         return "";
  21.       }
  22.     }
  23.   }
  24.  
  25.   public class SimpleXMLReader
  26.   {
  27.     public static XMLTag Parse(string xml)
  28.     {
  29.       XMLTag ret = new XMLTag();
  30.  
  31.       // Reading states
  32.       bool readingTag = false;
  33.       bool readingTagName = false;
  34.       bool readingAttributeName = false;
  35.       bool readingAttribute = false;
  36.       bool readingContent = false;
  37.  
  38.       // Temporary content
  39.       string currentTagName = "";
  40.       string currentAttributeName = "";
  41.       string currentAttribute = "";
  42.       string currentContent = "";
  43.  
  44.       // Loop through all the characters
  45.       for (int i = 0; i < xml.Length; i++) {
  46.         char c = xml[i];
  47.  
  48.         // Start of tag or start of end tag
  49.         if (c == '<') {
  50.           if (!readingTag) {
  51.             // Start of tag
  52.             readingTag = true;
  53.             readingTagName = true;
  54.             continue;
  55.           } else if (xml[++i] == '/') {
  56.             // Start of end tag, so we can stop reading here
  57.             ret.Content = currentContent;
  58.             break;
  59.           }
  60.         }
  61.  
  62.         // End of tag
  63.         if (c == '>') {
  64.           if (xml[i - 1] == '/') {
  65.             // There's no more content
  66.             readingTag = false;
  67.           } else {
  68.             // Read inner tag content
  69.             readingContent = true;
  70.           }
  71.           continue;
  72.         }
  73.  
  74.         // Seperator in between attributes and tag name
  75.         if (c == ' ') {
  76.           if (readingTagName) {
  77.             // Set the tag name
  78.             readingTagName = false;
  79.             ret.Name = currentTagName;
  80.           }
  81.  
  82.           // Regarding attributes
  83.           if (readingTag && !readingContent && !readingAttribute) {
  84.             readingAttributeName = true;
  85.             continue;
  86.           }
  87.         }
  88.  
  89.         // Definition character
  90.         if (c == '=') {
  91.           if (readingTag && readingAttributeName) {
  92.             readingAttributeName = false;
  93.             continue;
  94.           }
  95.         }
  96.  
  97.         // String
  98.         if (c == '"') {
  99.           if (!readingContent) {
  100.             if (!readingAttribute) {
  101.               // Start reading
  102.               readingAttribute = true;
  103.               continue;
  104.             } else {
  105.               // Stop reading
  106.               readingAttribute = false;
  107.  
  108.               // Add to attribute list
  109.               ret.Attributes.Add(currentAttributeName, currentAttribute);
  110.  
  111.               // Clear out
  112.               currentAttributeName = "";
  113.               currentAttribute = "";
  114.               continue;
  115.             }
  116.           }
  117.         }
  118.  
  119.         // Add character to string
  120.         if (readingTag) {
  121.           // Tag name
  122.           if (readingTagName) {
  123.             currentTagName += c;
  124.             continue;
  125.           }
  126.  
  127.           // Attribute value
  128.           if (readingAttribute) {
  129.             currentAttribute += c;
  130.             continue;
  131.           }
  132.  
  133.           // Attribute name
  134.           if (readingAttributeName) {
  135.             currentAttributeName += c;
  136.             continue;
  137.           }
  138.  
  139.           // Content
  140.           if (readingContent) {
  141.             currentContent += c;
  142.             continue;
  143.           }
  144.         }
  145.       }
  146.  
  147.       return ret;
  148.     }
  149.   }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement