gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:48:42 25 4
gpt4 key购买 nike

基于示例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>";

动态表单 = new DynamicXml(markup_fieldsOnly);

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

最佳答案

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

关于c# - 将 C# 动态对象序列化为 JSON 对象以供 javascript 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2553248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com