c# - Cant get list from received XML file -


this xml receive:

<?xml version="1.0" encoding="utf-8"?> <mdpr:data xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mdpr="http://..."> <mdpr:contactlist>     <mdpr:contact id="{123456}" classid="customer">       <mdpr:name>data1</mdpr:name>       <mdpr:transportcode>data2</mdpr:transportcode>       <mdpr:zipcode>data3</mdpr:zipcode>       <mdpr:city>data4</mdpr:city>     </mdpr:contact>     <mdpr:contact id="{234567}" classid="customer">       <mdpr:name>data5</mdpr:name>       <mdpr:transportcode>data6</mdpr:transportcode>       <mdpr:zipcode>data7</mdpr:zipcode>       <mdpr:city>data8</mdpr:city>     </mdpr:contact> </mdpr:contactlist> ... 

here how try contacts:

public class contact     {         public string name { get; set; }         public string transportcode { get; set; }     } ... xdocument xdoc = xdocument.load(doc.createnavigator().readsubtree());              list<contact> contacts = (from xml in xdoc.elements("contactlist").elements("contact")                                       select new contact                               {                                   name = xml.element("name").value,                                   transportcode = xml.element("transportcode").value                               }).tolist(); 

but nothing. doing wrong here?

you have mdpr namespace declared in xml:

xmlns:mdpr="http://..." 

but providing local name of elements in query. e.g. provide contactlist name, full name of element mdpr:contactlist. that's why nothing found.

you should define xnamespace namespace , use create full names of elements:

xnamespace mdpr = "http://..."; var contacts = c in xdoc.root.element(mdpr + "contactlist")                                   .elements(mdpr + "contact")                select new contact {                    transportcode = (string)c.element(mdpr + "transportcode"),                    name = (string)c.element(mdpr + "name")                }; 

also contactlist not root of document. should search under root.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -