我有一些代码,它将一个对象序列化为一个文件。我试图让每个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);
}
}
答案 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