title: Regex XML parsing C#
body: <p>I am trying to build a regex parser for a single XML block. </p>
  
  <p>I know people will say that Regex is not a good plan for xml, but I am working with stream data and I just need to know if a complete xml block has been broadcast and is sitting in the buffer.</p>
  
  <p>I am trying to handle for anything between the Opening and closing blocks of the XML and any data in parameters of the main block header. </p>
  
  <p>My example code is below the broken down Regular Expression, if anyone has any input on how to make this as comprehensive as possible I would greatly appreciate it.</p>
  
  <p>Here is my regular expression formatted for visual aid.</p>
  
  <p>I am balancing the  group, as well as the  group and validating that they do not exist at the end of the expression segments.</p>
  
  <pre><code>/*
     ^(?&lt;TAG&gt;[&lt;]
          (?![?])
          (?&lt;TAGNAME&gt;[^\s/&gt;]*)
      )
      (?&lt;ParamData&gt;
          (
              (\"
                  (?&gt;
                      \\\"|
                      [^"]|
                      \"(?&lt;quote&gt;)|
                      \"(?&lt;-quote&gt;)
                  )*
                  (?(quote)(?!))
                  \"
              )|
              [^/&gt;]
          )*?
      )
      (?:
          (?&lt;HASCONTENT&gt;[&gt;])|
          (?&lt;-TAG&gt;
              (?&lt;TAGEND&gt;/[&gt;])
          )
      )
      (?(HASCONTENT)
          (
              (?&lt;CONTENT&gt;
                  (
                      (?&lt;inTAG&gt;[&lt;]\&lt;TAGNAME&gt;)(?&lt;-inTAG&gt;/[&gt;])?|
                      (?&lt;-inTAG&gt;[&lt;]/\&lt;TAGNAME&gt;[&gt;])|
                      ([^&lt;]+|[&lt;](?![/]?\&lt;TAGNAME&gt;))
                  )*?
                  (?(inTAG)(?!))
              )
          )
          (?&lt;TAGEND&gt;(?&lt;-TAG&gt;)[&lt;]/\&lt;TAGNAME&gt;[&gt;])
      )
      (?(TAG)(?!))
  */
  </code></pre>
  
  <p>Within my class, I expect that any Null object returned means there was no xml block on the queue.</p>
  
  <p>Here is the class I am using.</p>
  
  <p>(I used a literal string (@"") to limit the escape requirements, All " characters were replaced with "" to format properly.</p>
  
  <pre><code>public class XmlDataParser
  {
      // xmlObjectExpression defined below to limit code highlight errors
      private Regex _xmlRegex;
      private Regex xmlRegex
      {
          get
          {
              if (_xmlRegex == null)
              {
                  _xmlRegex = new Regex(xmlObjectExpression);
              }
              return _xmlRegex;
          }
      }
  
      private string backingStore = "";
  
      public bool HasObject()
      {
          return (backingStore != null) &amp;&amp; xmlRegex.IsMatch(backingStore);
      }
      public string GetObject()
      {
          string result = null;
          if (HasObject())
          {
              lock (this)
              {
                  Match obj = xmlRegex.Match(backingStore);
                  result = obj.Value;
                  backingStore = backingStore.Substring(result.Length);
              }
          }
          return result;
      }
  
      public void AddData(byte[] bytes)
      {
          lock (this)
          {
              backingStore += System.Text.Encoding.Default.GetString(bytes);
          }
      }
  
      private static string xmlObjectExpression = @"^(?&lt;TAG&gt;[&lt;](?![?])(?&lt;TAGNAME&gt;[^\s/&gt;]*))(?&lt;ParamData&gt;((\""(?&gt;\\\""|[^""]|\""(?&lt;quote&gt;)|\""(?&lt;-quote&gt;))*(?(quote)(?!))\"")|[^/&gt;])*?)(?:(?&lt;HASCONTENT&gt;[&gt;])|(?&lt;-TAG&gt;(?&lt;TAGEND&gt;/[&gt;])))(?(HASCONTENT)((?&lt;CONTENT&gt;((?&lt;inTAG&gt;[&lt;]\&lt;TAGNAME&gt;)(?&lt;-inTAG&gt;/[&gt;])?|(?&lt;-inTAG&gt;[&lt;]/\&lt;TAGNAME&gt;[&gt;])|([^&lt;]+|[&lt;](?![/]?\&lt;TAGNAME&gt;)))*?(?(inTAG)(?!))))(?&lt;TAGEND&gt;(?&lt;-TAG&gt;)[&lt;]/\&lt;TAGNAME&gt;[&gt;]))(?(TAG)(?!))";
  
  
  
  }
  </code></pre>