我将日志存储在xml文件中...
在传统的直接文本格式方法中,您通常只需要一个openFile ...然后使用writeLine方法......
如何将新条目添加到xml文档结构中,就像使用文本文件方法一样?
使用 XmlWriter。
示例代码:
public class Quote
{
public string symbol;
public double price;
public double change;
public int volume;
}
public void Run()
{
Quote q = new Quote
{
symbol = "fff",
price = 19.86,
change = 1.23,
volume = 190393,
};
WriteDocument(q);
}
public void WriteDocument(Quote q)
{
var settings = new System.Xml.XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent= true
};
using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", q.symbol);
writer.WriteElementString("Price", XmlConvert.ToString(q.price));
writer.WriteElementString("Change", XmlConvert.ToString(q.change));
writer.WriteElementString("Volume", XmlConvert.ToString(q.volume));
writer.WriteEndElement();
}
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
<Stock Symbol="fff">
<Price>19.86</Price>
<Change>1.23</Change>
<Volume>190393</Volume>
</Stock>
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅 使用 XmlWriter 进行编写。
| 归档时间: |
|
| 查看次数: |
7805 次 |
| 最近记录: |