1. <i id='vmfoN'><tr id='vmfoN'><dt id='vmfoN'><q id='vmfoN'><span id='vmfoN'><b id='vmfoN'><form id='vmfoN'><ins id='vmfoN'></ins><ul id='vmfoN'></ul><sub id='vmfoN'></sub></form><legend id='vmfoN'></legend><bdo id='vmfoN'><pre id='vmfoN'><center id='vmfoN'></center></pre></bdo></b><th id='vmfoN'></th></span></q></dt></tr></i><div id='vmfoN'><tfoot id='vmfoN'></tfoot><dl id='vmfoN'><fieldset id='vmfoN'></fieldset></dl></div>
  2. <tfoot id='vmfoN'></tfoot>

        <bdo id='vmfoN'></bdo><ul id='vmfoN'></ul>
    1. <small id='vmfoN'></small><noframes id='vmfoN'>

      <legend id='vmfoN'><style id='vmfoN'><dir id='vmfoN'><q id='vmfoN'></q></dir></style></legend>

      强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值

      Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)

              <bdo id='ysTrQ'></bdo><ul id='ysTrQ'></ul>
            • <small id='ysTrQ'></small><noframes id='ysTrQ'>

            • <tfoot id='ysTrQ'></tfoot>
                <tbody id='ysTrQ'></tbody>
              <legend id='ysTrQ'><style id='ysTrQ'><dir id='ysTrQ'><q id='ysTrQ'></q></dir></style></legend>

                <i id='ysTrQ'><tr id='ysTrQ'><dt id='ysTrQ'><q id='ysTrQ'><span id='ysTrQ'><b id='ysTrQ'><form id='ysTrQ'><ins id='ysTrQ'></ins><ul id='ysTrQ'></ul><sub id='ysTrQ'></sub></form><legend id='ysTrQ'></legend><bdo id='ysTrQ'><pre id='ysTrQ'><center id='ysTrQ'></center></pre></bdo></b><th id='ysTrQ'></th></span></q></dt></tr></i><div id='ysTrQ'><tfoot id='ysTrQ'></tfoot><dl id='ysTrQ'><fieldset id='ysTrQ'></fieldset></dl></div>
              • 本文介绍了强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                Newtonsoft.Json.JsonConvert 类中的 SerializeXmlNode 函数在序列化过程中总是将 XML 的最后一个子节点的值输出为字符串类型,有时有时您可能需要将它们序列化为整数或布尔值.

                The SerializeXmlNode function from Newtonsoft.Json.JsonConvert class always outputs the value of the last child nodes of a XML as a string type in the serialization process, when sometimes you might need them to be serialized as an Integer or a Boolean.

                示例代码:

                <Object>
                  <ID>12</ID>
                  <Title>mytitle</Title>
                  <Visible>false</Visible>
                </Object>
                

                输出:

                { "ID" : "12",
                  "Title" : "mytitle",
                  "Visible" : "false"
                }
                

                期望的输出:

                { "ID" : 12,
                  "Title" : "mytitle",
                  "Visible" : false
                }
                

                有没有办法强制将 XML 节点序列化为整数或布尔值?

                Is there a way to force a XML node to be serialized as a Integer or a Boolean?

                谢谢.

                注意:当 XML 已经序列化为 JSON 字符串时,请避免发布解决方法,因为这些解决方法是我们愿意避免的.

                Note: Please avoid posting workarounds when the XML is already serialized to a JSON string, as those workarounds are the ones that we are willing to avoid.

                推荐答案

                当前的 JSON.NET 构建不提供请求的功能,所以我修改了源代码以提供此功能:

                The current JSON.NET build doesn't provide the requested feature, so I modified the source code to provide this functionality:

                https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs

                此修改为 XmlNodeConverter 提供了一种从 XML 节点读取可选属性的方法,该属性称为类型",该属性包含所需的节点值序列化.默认情况下,转换器将结果 JSON 字符串中的所有值序列化为字符串,但现在您可以添加一个属性来指定所需的 DataType 输出.允许的类型为 Integer、Float、Boolean 和 Date.

                This modification provides the XmlNodeConverter a way to read an optional attribute from XML nodes called "Type" that holds the desired serialization of a node value. By default, the converter serializes all values as string in the result JSON string, but now you can add an attribute that specifies the desired DataType output. The allowed types are Integer, Float, Boolean and Date.

                例如,如果你有这个源 XML:

                For example, if you have this source XML:

                <Object>
                  <ID json:Type='Integer'>12</ID>
                  <Title>mytitle</Title>
                  <Visible json:Type='Boolean'>false</Visible>
                  <Price json:Type='Float'>1.55</Price>
                  <ExpirationDate json:Type='Date'>2013-12-31</ExpirationDate>
                </Object>
                

                会被序列化为:

                {
                    "ID":12,
                    "Title":"mytitle",
                    "Visible":false,
                    "Price":1.55,
                    "ExpirationDate":"2013-12-31T00:00:00"
                }
                

                这篇关于强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                c# JSON Serialization Use Value Instead of Property Name(c# JSON序列化使用值而不是属性名)
                <i id='fLpHS'><tr id='fLpHS'><dt id='fLpHS'><q id='fLpHS'><span id='fLpHS'><b id='fLpHS'><form id='fLpHS'><ins id='fLpHS'></ins><ul id='fLpHS'></ul><sub id='fLpHS'></sub></form><legend id='fLpHS'></legend><bdo id='fLpHS'><pre id='fLpHS'><center id='fLpHS'></center></pre></bdo></b><th id='fLpHS'></th></span></q></dt></tr></i><div id='fLpHS'><tfoot id='fLpHS'></tfoot><dl id='fLpHS'><fieldset id='fLpHS'></fieldset></dl></div>
              • <legend id='fLpHS'><style id='fLpHS'><dir id='fLpHS'><q id='fLpHS'></q></dir></style></legend>
                  <tbody id='fLpHS'></tbody>

                  <small id='fLpHS'></small><noframes id='fLpHS'>

                    <tfoot id='fLpHS'></tfoot>

                          <bdo id='fLpHS'></bdo><ul id='fLpHS'></ul>