title: Serialize C# dynamic object to JSON object to be consumed by javascript
body: <p>Based on the example <a href="http://www.gotnet.biz/Blog/post/Fluent-XML-Parsing-Using-CSharp-Dynamic-Type-Part-2.aspx" rel="nofollow noreferrer">c# dynamic with XML</a>, I modified DynamicXml.cs and parsed my xml string.
  the modified part is as follows</p>
  
  <pre><code>    public override bool TryGetMember(GetMemberBinder binder, out object result)
      {
          result = null;
          if (binder.Name == "Controls")
              result = new DynamicXml(_elements.Elements());
          else if (binder.Name == "Count")
              result = _elements.Count;
          else
          {
              var attr = _elements[0].Attribute(
                  XName.Get(binder.Name));
              if (attr != null)
                  result = attr.Value;
              else
              {
                  var items = _elements.Descendants(
                      XName.Get(binder.Name));
                  if (items == null || items.Count() == 0)
                      return false;
                  result = new DynamicXml(items);
              }
          }
          return true;
      }
  </code></pre>
  
  <p>The xml string to parse:</p>
  
  <pre><code>               "&lt; View runat='server' Name='Doc111'&gt;" +
                      "&lt; Caption Name='Document.ConvertToPdf' Value='Allow Conversion to PDF'&gt;&lt;/ Caption&gt;" +
                      "&lt; Field For='Document.ConvertToPdf' ReadOnly='False' DisplayAs='checkbox' EditAs='checkbox'&gt;&lt;/ Field&gt;" +
                      "&lt; Field For='Document.Abstract' ReadOnly='False' DisplayAs='label' EditAs='textinput'&gt;&lt;/ Field&gt;" +
                      "&lt; Field For='Document.FileName' ReadOnly='False' DisplayAs='label' EditAs='textinput'&gt;&lt;/ Field&gt;" +
                      "&lt; Field For='Document.KeyWords' ReadOnly='False' DisplayAs='label' EditAs='textinput'&gt;&lt;/ Field&gt;" +
                      "&lt; FormButtons SaveCaption='Save' CancelCaption='Cancel'&gt;&lt;/ FormButtons&gt;" +
                  "&lt;/ View&gt;";
  </code></pre>
  
  <p>dynamic form = new DynamicXml(markup_fieldsOnly);</p>
  
  <p>is there a way to serialize the content of this dynamic object(name value pairs inside dynamic) <strong>form</strong> as JSON object and sent to client side(browser)?</p>