c# - get xml node value from xml string -


i have xml contain xml namespace. need value xml node

<personxml:person xmlns:personxml="http://www.your.example.com/xml/person" xmlns:cityxml="http://www.my.example.com/xml/cities"> <personxml:name>rob</personxml:name> <personxml:age>37</personxml:age> <cityxml:homecity>     <cityxml:name>london</cityxml:name>     <cityxml:lat>123.000</cityxml:lat>     <cityxml:long>0.00</cityxml:long> </cityxml:homecity> 

now want value of tag <cityxml:lat> 123.00

code :

string xml = "<personxml:person xmlns:personxml='http://www.your.example.com/xml/person' xmlns:cityxml='http://www.my.example.com/xml/cities'><personxml:name>rob</personxml:name><personxml:age>37</personxml:age><cityxml:homecity><cityxml:name>london</cityxml:name><cityxml:lat>123.000</cityxml:lat><cityxml:long>0.00</cityxml:long></cityxml:homecity></personxml:person>"; var elem = xelement.parse(xml); var value = elem.element("ota_personxml/cityxml:homecity").value; 

error getting

the '/' character, hexadecimal value 0x2f, cannot included in name. 

you need use xnamespace. example:

xnamespace ns1 = "http://www.your.example.com/xml/person"; xnamespace ns2 = "http://www.my.example.com/xml/cities";  var elem = xelement.parse(xml); var value = elem.element(ns2 + "homecity").element(ns2 + "name").value;  //value = "london" 

create xnamespace using string contains uri, combine namespace local name.

for more information, see here.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -