Xmltextwriter usage Diary

Source: Internet
Author: User

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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.