如何在XmlTextWriter中设置Settings属性,以便我可以在自己的行上编写每个XML属性?

时间:2011-11-23 04:03:48

标签: c# xml serialization xmlwriter xmltextwriter

我有一些代码,它将一个对象序列化为一个文件。我试图让每个XML属性在一个单独的行上输出。代码如下所示:

public static void ToXMLFile(Object obj, string filePath)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.NewLineOnAttributes = true;

    XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
    writer.Settings = settings; // Fails here.  Property is read only.

    using (Stream baseStream = writer.BaseStream)
    {
        serializer.Serialize(writer, obj);
    }
}

唯一的问题是,Settings对象的XmlTextWriter属性是只读的。

如何在Settings对象上设置XmlTextWriter属性,以便NewLineOnAttributes设置有效?


好吧,我认为我需要一个XmlTextWriter,因为XmlWriter是一个abstract类。如果你问我,有点困惑。 最终工作代码在这里:

/// <summary>
/// Serializes an object to an XML file; writes each XML attribute to a new line.
/// </summary>
public static void ToXMLFile(Object obj, string filePath)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.NewLineOnAttributes = true;

    using (XmlWriter writer = XmlWriter.Create(filePath, settings))
    {
        serializer.Serialize(writer, obj);
    }
}

2 个答案:

答案 0 :(得分:19)

使用Create()的静态XmlWriter方法。

XmlWriter.Create(filePath, settings);

请注意,您可以在设置中设置NewLineOnAttributes属性。

答案 1 :(得分:3)

我知道问题已经过时了,无论如何它实际上可以为Sub BindData(ByVal BalanceDate As Date) Try Dim CAMSClass As New CFPS_BLL.CAMS_Class Dim dtList As DataTable = CAMSClass.Get_CAMS_Balancing(BalanceDate) If dtList.Rows.Count() = 0 Then lblNumRows.Text = "No data found" gvAccSumm.Visible = False btnSave.Visible = False btnUpdate2.Visible = False Else btnSave.Visible = True btnUpdate2.Visible = True gvAccSumm.DataSource = dtList Dim dRow As DataRow dRow = dtList.Rows(0) gvAccSumm.DataBind() gvAccSumm.Visible = True 'if date is last day of the previous month, they should still be able to update balances for month end If txtDate.Text = DateAdd(DateInterval.Day, -1, CDate("01/" & Month(Date.Today) & "/" & Year(Date.Today))) Then 'lblMessage.Text = "This date is the last day of the previous month" btnSave.Enabled = True btnSave.ToolTip = "Button enabled to do month end balance" btnUpdate2.Enabled = True btnUpdate2.ToolTip = "Button enabled to do month end balance" 'All other days of month, can only capture cams balances for the previous day - 'cannot change balances for other days ElseIf txtDate.Text < DateAdd(DateInterval.Day, -2, Date.Today.Date) Then btnSave.Enabled = False btnSave.ToolTip = "If this button is not available then you cannot update the balances for the selected day" btnUpdate2.Enabled = False btnUpdate2.ToolTip = "If this button is not available then you cannot update the balances for the selected day" Else btnSave.Enabled = True btnSave.ToolTip = "click to save" btnUpdate2.Enabled = True btnUpdate2.ToolTip = "click to save" End If End If CAMSClass = Nothing Catch ex As Exception Throw ex End Try End Sub 设置缩进。与XMLTextWriter不同,您不必通过设置;你应该使用XMLwriter属性:

Formatting

请参阅 https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.formatting(v=vs.110).aspx