Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 写入SML XmlSerializer c_C#_Xmlserializer - Fatal编程技术网

C# 写入SML XmlSerializer c

C# 写入SML XmlSerializer c,c#,xmlserializer,C#,Xmlserializer,我在MSDN上找到了这个示例代码,但代码的问题是它在一行中写入所有XML。 我要缩进和换行。 我不知道如何在代码中插入XmlWriterSettings。 请帮我做这个 提前谢谢 using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class

我在MSDN上找到了这个示例代码,但代码的问题是它在一行中写入所有XML。 我要缩进和换行。 我不知道如何在代码中插入XmlWriterSettings。 请帮我做这个

提前谢谢

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized. 
public class OrderedItem
{
   public string ItemName;
   public string Description;
   public decimal UnitPrice;
   public int Quantity;
   public decimal LineTotal;
   // A custom method used to calculate price per item. 
   public void Calculate()
   {
      LineTotal = UnitPrice * Quantity;
   }
}

public class Test{
   public static void Main()
   {
      Test t = new Test();
      // Write a purchase order.
      t.SerializeObject("simple.xml");
   }

   private void SerializeObject(string filename)
   {
      Console.WriteLine("Writing With XmlTextWriter");

      XmlSerializer serializer = 
      new XmlSerializer(typeof(OrderedItem));
      OrderedItem i = new OrderedItem();
      i.ItemName = "Widget";
      i.Description = "Regular Widget";
      i.Quantity = 10;
      i.UnitPrice = (decimal) 2.30;
      i.Calculate();
      // Create an XmlTextWriter using a FileStream.
      Stream fs = new FileStream(filename, FileMode.Create);
      XmlWriter writer = 
      new XmlTextWriter(fs, Encoding.Unicode);
      // Serialize using the XmlTextWriter.
      serializer.Serialize(writer, i);
      writer.Close();
   }
}

我真的不知道这有什么关系。不过,我猜这一定是你的编辑。默认情况下,XMLSerializer会缩进XML。如果需要更改任何设置,则XmlWriter.Create方法会有一个重载,该方法将XmlWriterSettings作为参数。

在调用之前,需要在类中设置适当的属性

此外,我还建议进行以下额外更改:

Stream和XmlWriter都是,因此应该包装在语句中,以防在写入文件时引发异常

为清楚起见,请从代码中分离出一个通用方法来序列化任何对象,以计算和编写OrderedItem类

因此:


我尝试了XmlWriter.Create方法,但没有成功
    private void SerializeObject(string filename)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem));
        OrderedItem i = new OrderedItem();
        i.ItemName = "Widget";
        i.Description = "Regular Widget";
        i.Quantity = 10;
        i.UnitPrice = (decimal)2.30;
        i.Calculate();

        Console.WriteLine(string.Format("Writing \"{0}\" With XmlTextWriter", filename));
        SerializeObject(i, filename);
    }

    public static void SerializeObject<T>(T obj, string filename)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        using (var fs = new FileStream(filename, FileMode.Create))
        {
            var settings = new XmlWriterSettings() { Indent = true, IndentChars = "    ", Encoding = Encoding.Unicode };
            using (var writer = XmlWriter.Create(fs, settings))
            {
                serializer.Serialize(writer, obj);
            }
        }
    }