【问题标题】:C# Serialize object into xml without formattingC#将对象序列化为xml而不格式化
【发布时间】:2020-02-15 10:07:44
【问题描述】:

我需要将一个 c# 对象序列化为 xml,我可以像在格式化部分中那样做,但是我们可以在没有缩进/额外空格/新行的格式化的情况下实现它。这是必需的,因为我们需要将整个对象写入 csv,因此我们需要一个缩小版本。我尝试了几个 XmlWriterSettings,但没有按预期工作。任何代码 sn -p 都会有很大帮助,并且要序列化的对象可以具有其他引用类型作为可能从其他基类继承的成员,因此序列化程序可能需要已知类型

格式化的 XML:

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

无格式:

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

代码尝试:

Type[] _knownExpressions = new Type[]
{
     typeof(SimpleExpression),
     typeof(CompositeExpression)
};
string expression = string.Empty;
MemoryStream ms = new MemoryStream();
DataContractSerializer dcs = new DataContractSerializer(typeof(Expression), _knownExpressions);                                        
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, System.Text.Encoding.Default))
{
      xmlTextWriter.Formatting = Formatting.None;
      dcs.WriteObject(xmlTextWriter, expression);
      xmlTextWriter.Flush();
      xmlTextWriter.BaseStream.Position = 0;
      StreamReader sr = new StreamReader(xmlTextWriter.BaseStream);
      expression = sr.ReadToEnd();
      sr.Close();
 }

【问题讨论】:

  • 请您出示您已经在使用的代码吗? minimal reproducible example 是理想的选择。
  • 如何逐行读取并添加到 StringBuilder 并稍后使用它保存在 csv 中?

标签: c# xml serialization xml-serialization


【解决方案1】:

别介意,我没有将正确的对象传递给序列化程序,以下是有效的

Type[] _knownExpressions = new Type[]
{
     typeof(SimpleExpression),
     typeof(CompositeExpression)
};
string expression = string.Empty;
Expression exp = new Expression(){
   // Fill the object
};
MemoryStream ms = new MemoryStream();
DataContractSerializer dcs = new DataContractSerializer(typeof(Expression), _knownExpressions);                                        
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, System.Text.Encoding.Default))
{
      xmlTextWriter.Formatting = Formatting.None;
      dcs.WriteObject(xmlTextWriter, exp);
      xmlTextWriter.Flush();
      xmlTextWriter.BaseStream.Position = 0;
      StreamReader sr = new StreamReader(xmlTextWriter.BaseStream);
      expression = sr.ReadToEnd();
      sr.Close();
 }

【讨论】:

    【解决方案2】:

    Xml Writer 默认为 Ident = false。所以以下将起作用:

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication137
    {
        class Program
        {
            const string INPUT_FILENAME = @"c:\temp\test.xml";
            const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(INPUT_FILENAME);
                XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME);
                doc.WriteTo(writer);
                writer.Flush();
                writer.Close();
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2018-10-11
      • 2015-02-16
      相关资源
      最近更新 更多