Today, I used the XML file operation in Win8 metro and found that it was very important to sort it out. Although its usage was simple, it was a little difficult to use each time.
Read XML files
Windows. Storage. storagefolder = await windows. ApplicationModel. Package. Current. installedlocation. getfolderasync (folder );
Windows. Storage. storagefile = await storagefolder. getfileasync (File );
Windows. Data. xml. Dom. xmlloadsettings = new xmlloadsettings ();
Xmlloadsettings. prohibitdtd = false;
Xmlloadsettings. resolveexternals = false;
Xmlloadsettings. elementcontentwhitespace = true;
Xmldocument Doc = await windows. Data. xml. Dom. xmldocument. loadfromfileasync (storagefile, xmlloadsettings );
String xmldata = Doc. getxml ();
Shown as follows:
Add Node
// Add nodes
VaR nodelist = Doc. selectnodes ("/RSS/channel ");
For (INT I = 0; I <3; I ++)
{
Xmlelement ele1 = Doc. createelement ("channeltype ");
Xmlelement e1 = Doc. createelement ("channelid ");
E1.innertext = "no" + I. tostring ();
Ele1.appendchild (E1 );
Xmlelement e2 = Doc. createelement ("channelname ");
E2.innertext = "channel" + I. tostring ();
Ele1.appendchild (E2 );
Nodelist [0]. appendchild (ele1 );
}
Await Doc. savetofileasync (storagefile );
The result is as follows:
Query and modify corresponding nodes
VaR node = nodelist [0]. selectnodes ("channeltype ");
Node [0]. selectsinglenode ("channelid"). innertext = "channel12 ";
Await Doc. savetofileasync (storagefile );
The result is as follows:
Delete a node
VaR nodemain = nodelist [0]. selectnodes ("channeltype") [0];
VaR noderemove = nodemain. selectsinglenode ("channelid ");
Nodemain. removechild (noderemove );
Await Doc. savetofileasync (storagefile );
Result:
The above is a simple XML operation, and the layout is awkward. After two code plug-ins are installed, the plug-ins cannot be installed. It is very tangled. I will look for some poor code plug-ins, people's code layout is beautiful, and my call ~~