• <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 格式化输出

        时间:2023-05-21
        <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 格式化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:根据可用宽度和字体计算文本高度? 下一篇:如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?

                  相关文章

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

                    <legend id='0yTtG'><style id='0yTtG'><dir id='0yTtG'><q id='0yTtG'></q></dir></style></legend>
                      • <bdo id='0yTtG'></bdo><ul id='0yTtG'></ul>
                    1. <tfoot id='0yTtG'></tfoot>
                    2. <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>