开发者

Serialize C# dynamic object to JSON object to be consumed by javascript

Based on the example c# dynamic with XML, I modified DynamicXml.cs and parsed my xml string. the modified part is as follows

    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;
    }

The xml string to parse:

               "< View runat='server' Name='Doc111'>" +
                    "< Caption Name='Document.ConvertToPdf' Value='Allow Conversion to PDF'></ Caption>" +
                    "< Field For='Document.ConvertToPdf' ReadOnly='False' DisplayAs='checkbox' EditAs='checkbox'></ Field>" +
                    "< Field For='Document.Abstract' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< Field For='Document.FileName' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< Field For='Document.KeyWords' ReadOnly='False' DisplayAs='label' EditAs='textinput'></ Field>" +
                    "< FormButtons SaveCaption='Save' CancelCaption='Cancel'></ FormButtons>" +
                "</ View>";

dynamic form = new DynamicXml(markup_fieldsOnly);

is there a way to serialize the content of this dynamic object(name value pairs inside dynamic) form as JSON object and sent to client side(browser)?


I've heard Json.Net works pretty well, though I've never used it myself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜