XmlTextWriter的使用日记
使用XmlTextWriter生成如下一个xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Pkboy kind="site">
<command name="price" object="game" password="asdfasdf">
<game name="魔兽世界">
<server name="魔兽服务器一号">
<item name="金币" kind="gold" quantity="12" gross="0.5005" maxdollar="99" mindollar="88" rate="6.88" />
</server>
</game>
</command>
</Pkboy>
使用的代码:
public static string GeneratePriceXml(PriceStruct ps, Encoding encode)
{
string xml = string.Empty;
using (MemoryStream ms = new MemoryStream())
{
using (XmlTextWriter xtw = new XmlTextWriter(ms, encode))
{
xtw.Formatting = Formatting.Indented;
//xtw.Indentation = 2;
xtw.WriteStartDocument(true);
//Pkboy node
xtw.WriteStartElement("Pkboy");
xtw.WriteAttributeString("kind", "site");
//command node
xtw.WriteStartElement("command");
xtw.WriteAttributeString("name", "price");
xtw.WriteAttributeString("object", "game");
xtw.WriteAttributeString("password", ps.password);
//game node
xtw.WriteStartElement("game");
xtw.WriteAttributeString("name", ps.gameName);
// server node
xtw.WriteStartElement("server");
xtw.WriteAttributeString("name", ps.serverName);
// item node
xtw.WriteStartElement("item");
xtw.WriteAttributeString("name", ps.itemName);
xtw.WriteAttributeString("kind", ps.itemKind);
xtw.WriteAttributeString("quantity", ps.quantity.ToString());
xtw.WriteAttributeString("gross", ps.gross.ToString());
xtw.WriteAttributeString("maxdollar", ps.maxDollar.ToString());
xtw.WriteAttributeString("mindollar", ps.minDollar.ToString());
xtw.WriteAttributeString("rate", ps.rate.ToString());
// colse item node
xtw.WriteEndElement();
// close server node
xtw.WriteEndElement();
// close game node
xtw.WriteEndElement();
// close command node
xtw.WriteEndElement();
// close pkboy node
xtw.WriteEndElement();
// close document
xtw.WriteEndDocument();
xtw.Flush();
xml = encode.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
return xml;
}
产生xml 如下格式:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Pkboy kind="result">
<result success="false">
<data>this is result data</data>
<error kind="DatabaseError">
<message>数据库链接失败</message>
</error>
</result>
</Pkboy>
采用代码:
public static string GenerateResultXml(ResultStruct rs, Encoding encode)
{
string xml = string.Empty;
using (MemoryStream ms = new MemoryStream())
{
using (XmlTextWriter xtw = new XmlTextWriter(ms, encode))
{
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument(true);
//Pkboy node
xtw.WriteStartElement("Pkboy");
xtw.WriteAttributeString("kind", "result");
//result node
xtw.WriteStartElement("result");
if (rs.success)
{
xtw.WriteAttributeString("success", "true");
}
else
{
xtw.WriteAttributeString("success", "false");
}
//data node
xtw.WriteStartElement("data");
xtw.WriteValue(rs.data);
xtw.WriteEndElement();
// error node
xtw.WriteStartElement("error");
xtw.WriteAttributeString("kind", Enum.GetName(typeof(ErrorKind), rs.errorKind));
// message node
xtw.WriteStartElement("message");
xtw.WriteValue(rs.errorMessage);
xtw.WriteEndElement();
// close error node
xtw.WriteEndElement();
// close result node
xtw.WriteEndElement();
// close document
xtw.WriteEndDocument();
xtw.Flush();
xml = encode.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
return xml;
}