Notes for xmltextreader

Source: Internet
Author: User

For 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>

 

Read using xmltextreader:

Byte [] buffer = encoding. utf8.getbytes (XML );
Using (Stream = new memorystream (buffer ))
{
Using (xmltextreader STR = new xmltextreader (Stream ))
{
Str. Read ();

Str. Read ();
Str. Read ();
Assert. areequal ("pkboy", str. Name );
Str. Read ();
Str. Read ();
Assert. areequal ("command", str. Name );
Str. Read ();
Str. Read ();
Assert. areequal ("game", str. Name );
Str. movetofirstattribute ();
Assert. areequal ("World of Warcraft", str. value );
Str. Read ();
Str. Read ();
Str. movetofirstattribute ();
Assert. areequal ("Warcraft Server 1", str. value );
}
}

 

The above test is normal, that is, after three read () times, it will arrive at the pkboy node. Why?

After tracking, after the first read (),

<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>, The name of the display node is XML, that is, the content in <> is treated

 

One node. Version and so on are all attributes. To obtain these attributes, you must use a group of methods similar to movetofirstattribute. Here, read () Again (),

 

Then, the document "\ r \ n" is displayed, that is, the second read () contains "\ r \ n". If there is a carriage return, it will be read () once as a node.

 

If "\ r \ n" is not included, you can directly reach the next node.

 

Therefore, because the carriage return contains a line break, the first element pkboy will be reached after three times.

 

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.