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