将C#动态对象序列化为javascript对象以供javascript使用

时间:2010-03-31 13:24:46

标签: c# json dynamic

基于示例c# dynamic with XML,我修改了DynamicXml.cs并解析了我的xml字符串。 修改后的部分如下

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

要解析的xml字符串:

               "< 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);

有没有办法将此动态对象的内容(动态内的名称值对)表单序列化为JSON对象并发送到客户端(浏览器)?

1 个答案:

答案 0 :(得分:1)

我听说Json.Net效果很好,但我自己从未使用过它。