c# - How to parse XML in a Windows Phone 7 application -
could tell me how parse xml-string receive wcf-rest service?
my webserive xml-string looks like
<ws> <info> <name>beta</name> <description>prototyps</description> </info> <pages> <page> <name>custom</name> <description>todo</description> </page> ...many other pages... </pages> </ws>
an phone sourcecode:
public void downloadcompleted(object sender, downloadstringcompletedeventargs e) { if (!e.cancelled && e.error == null) { var reply = xelement.parse(e.result).descendants("ws"); // null ... } }
if seek parse through xdocument.load(e.result) exception: file not found.
i want "unique" info of info-node , list of page-nodes values
update if seek load root-element via var item = xdoc.root.descendants(); item assigned whole xml-file.
update 2 seems problem occurs namespaces in root-element. namespaces xdocument parse webservice output not correctly. if delete namespaces works fine. explain me issue? , there handy solution deleting namespaces?
update 3 handy way removing namespaces1
with simple xml if know format wont change, might interested in using xpath:
var xdoc = xdocument.parse(e.result); var name = xdoc.xpathselectelement("/ws/info/name");
but multiple pages, maybe linq xml
var xdoc = xdocument.parse(xml); var pages = xdoc.descendants("pages").single(); var pageslist = pages.elements().select(x => new page((string)x.element("name"), (string)x.element("description"))).tolist();
where page simple class:
public class page { public string name { get; set; } public string descrip { get; set; } public page(string name, string descrip) { name = name; descrip = descrip; } }
let me know if need more explanation.
also select info without xpath:
var info = xdoc.descendants("info").single(); var infoname = info.element("name").value; var infodescrip = info.element("description").value;
c# windows-phone-7 xml-parsing webservice-client
No comments:
Post a Comment