• <i id='4tO1O'><tr id='4tO1O'><dt id='4tO1O'><q id='4tO1O'><span id='4tO1O'><b id='4tO1O'><form id='4tO1O'><ins id='4tO1O'></ins><ul id='4tO1O'></ul><sub id='4tO1O'></sub></form><legend id='4tO1O'></legend><bdo id='4tO1O'><pre id='4tO1O'><center id='4tO1O'></center></pre></bdo></b><th id='4tO1O'></th></span></q></dt></tr></i><div id='4tO1O'><tfoot id='4tO1O'></tfoot><dl id='4tO1O'><fieldset id='4tO1O'></fieldset></dl></div>

      <small id='4tO1O'></small><noframes id='4tO1O'>

      • <bdo id='4tO1O'></bdo><ul id='4tO1O'></ul>

      <tfoot id='4tO1O'></tfoot>
      <legend id='4tO1O'><style id='4tO1O'><dir id='4tO1O'><q id='4tO1O'></q></dir></style></legend>

        防止 XmlSerializer 格式化输出

        Prevent XmlSerializer from formatting output(防止 XmlSerializer 格式化输出)
        <i id='SVEA0'><tr id='SVEA0'><dt id='SVEA0'><q id='SVEA0'><span id='SVEA0'><b id='SVEA0'><form id='SVEA0'><ins id='SVEA0'></ins><ul id='SVEA0'></ul><sub id='SVEA0'></sub></form><legend id='SVEA0'></legend><bdo id='SVEA0'><pre id='SVEA0'><center id='SVEA0'></center></pre></bdo></b><th id='SVEA0'></th></span></q></dt></tr></i><div id='SVEA0'><tfoot id='SVEA0'></tfoot><dl id='SVEA0'><fieldset id='SVEA0'></fieldset></dl></div>
          • <small id='SVEA0'></small><noframes id='SVEA0'>

          • <legend id='SVEA0'><style id='SVEA0'><dir id='SVEA0'><q id='SVEA0'></q></dir></style></legend>
              <bdo id='SVEA0'></bdo><ul id='SVEA0'></ul>

                  <tbody id='SVEA0'></tbody>
                  <tfoot id='SVEA0'></tfoot>

                  本文介绍了防止 XmlSerializer 格式化输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  当使用 XmlSerializer 的默认设置时,它会将 XML 输出为格式化值.

                  When using the default settings with the XmlSerializer it will output the XML as a formated value.

                  IE:类似的东西.

                  <?xml version="1.0" encoding="utf-8"?>
                  <ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <Stock>
                      <ProductCode>12345</ProductCode>
                      <ProductPrice>10.32</ProductPrice>
                    </Stock>
                    <Stock>
                      <ProductCode>45632</ProductCode>
                      <ProductPrice>5.43</ProductPrice>
                    </Stock>
                  </ArrayOfStock>
                  

                  如何防止输出中出现任何类型的格式化?所以我想要实现的就是这个.

                  How does one prevent any type of formatting on the output? So what I am looking to achieve is this.

                  <?xml version="1.0" encoding="utf-8"?><ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Stock><ProductCode>123456</ProductCode><ProductPrice>10.57</ProductPrice></Stock><Stock><ProductCode>789123</ProductCode><ProductPrice>133.22</ProductPrice></Stock></ArrayOfStock>
                  

                  我的方法的完整代码是

                  public static String Serialize(Stock stock)
                  {
                       XmlSerializer serializer = new XmlSerializer(typeof(Stock));
                  
                       using (StringWriter stringWriter = new StringWriter())
                       {
                           serializer.Serialize(stringWriter, stock);
                           return stringWriter.ToString();
                       }            
                  }
                  

                  推荐答案

                  不是很直观,但是 XmlWriterSettings 上的 Indent 属性控制了整个格式:

                  Not very intuitive, but the Indent property on the XmlWriterSettings controls the whole formatting:

                  var serializer = new XmlSerializer(typeof(MyClass));
                  
                  using (var writer = new StreamWriter("file.path"))
                  using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false }))
                  {
                      serializer.Serialize(xmlWriter, myObject);
                  }
                  

                  XmlWriterSettings 您可能想要探索.

                  There are a few more options on XmlWriterSettings that you might want to explore.

                  这篇关于防止 XmlSerializer 格式化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 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 自定义合约序列化和集合)

                  <small id='0yTtG'></small><noframes id='0yTtG'>

                      <tbody id='0yTtG'></tbody>
                      <legend id='0yTtG'><style id='0yTtG'><dir id='0yTtG'><q id='0yTtG'></q></dir></style></legend>
                          • <bdo id='0yTtG'></bdo><ul id='0yTtG'></ul>
                          • <tfoot id='0yTtG'></tfoot>
                          • <i id='0yTtG'><tr id='0yTtG'><dt id='0yTtG'><q id='0yTtG'><span id='0yTtG'><b id='0yTtG'><form id='0yTtG'><ins id='0yTtG'></ins><ul id='0yTtG'></ul><sub id='0yTtG'></sub></form><legend id='0yTtG'></legend><bdo id='0yTtG'><pre id='0yTtG'><center id='0yTtG'></center></pre></bdo></b><th id='0yTtG'></th></span></q></dt></tr></i><div id='0yTtG'><tfoot id='0yTtG'></tfoot><dl id='0yTtG'><fieldset id='0yTtG'></fieldset></dl></div>