Use xmltextwriter to generate the following XML:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Pkboy kind = "Site">
<Command name = "price" Object = "game" Password = "ASDFASDF">
<Game name = "World of Warcraft">
<Server name = "Warcraft Server 1">
<Item name = "gold coin" kind = "gold" quantity = "12" gross = "0.5005" maxdollar = "99" mindollar = "88" rate = "6.88"/>
</Server>
</Game>
</Command>
</Pkboy>
UsedCode:
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 the 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;
}
The XML format is as follows:
<? 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> database connection failed </message>
</Error>
</Result>
</Pkboy>
Code used:
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;
}