Two Methods for parsing XML in Windows phone7

Source: Internet
Author: User

For XML data parsing, The WP7 platform does not provide the common parser saxparser and domparser. If you used to develop Android applications, you may find that parsing XML on Windows Phone 7 still needs. NET framework, such as xmlreader and LINQ to XML parser.

1. xml Reader

Xmlreader provides fast resolution and saves memory, but read-only XML content cannot be modified without the cache resolution function. You can use the saxparser class for parsing on the Android platform, xmlreader is similar to saxparser. The following is a simple example.

Stringbuilder output = new stringbuilder ();

String xmlstring = @ "<? XML version = '1. 0'?>
<Items>
<Item> sub element <more/> cwj </item>
</Items> ";

// Create an xmlreader instance, which is similar to the saxparser instance created on the Android platform through saxparserfactory.
Using (xmlreader reader = xmlreader. Create (New stringreader (xmlstring )))
{
Xmlwritersettings Ws = new xmlwritersettings ();
WS. indent = true;
 
Using (xmlwriter writer = xmlwriter. Create (output, WS ))
{

While (reader. Read ())
{
Switch (reader. nodetype)
{
Case xmlnodetype. element: // similar to startelement () in the android saxparser class.
Writer. writestartelement (reader. Name );
Break;
Case xmlnodetype. Text: // parse node content
Writer. writestring (reader. value );
Break;
Case xmlnodetype. xmldeclaration:
Case xmlnodetype. processinginstruction: // resolution Declaration
Writer. writeprocessinginstruction (reader. Name, reader. value );
Break;
Case xmlnodetype. Comment: // parse comments
Writer. writecomment (reader. value );
Break;
Case xmlnodetype. endelement: // similar to endelement () in the saxparser class

Writer. writefullendelement ();
Break;
}
}
}
}

2. LINQ to XML

This is similar to the DOM parser in MSXML used on traditional Win32. It maps the entire XML file to the memory, making it easier to locate and edit XML. To create an XML file, use the following method,

Stringbuilder output = new stringbuilder ();

Xdocument srctree = new xdocument (
New xcomment ("comment content "),
New xelement ("root ",
New xelement ("Child1", "data1 "),
New xelement ("child2", "data2 "),
New xelement ("Child3", "data3 "),
New xelement ("child2", "data4 "),
New xelement ("info5", "info5 "),
New xelement ("info6", "info6 "),
New xelement ("info7", "info7 "),
New xelement ("info8", "info8 ")
)
);

Xdocument Doc = new xdocument (
New xcomment ("comment "),
New xelement ("root ",
From El in srctree. element ("root"). Elements ()
Where (string) El). startswith ("data ")
Select el
)
); // The syntax here is particularly similar to root. getelementsbytagname (item) in traditional Dom );

Output. append (Doc + environment. newline );
Outputtextblock. Text = output. tostring ();

Article from

WP7 developer: http://dev.ruanman.net

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.