c# - XmlReader can't parse XML with <qn:xxxxx> format? -


i trying parse following xml string:

<qn:querynotification xmlns:qn="http://schemas.microsoft.com/sql/notifications/querynotification" id="307" type="change" source="data" info="insert" database_id="6" sid="0x010500000000000515000000aea63bde2de94b9ff38541a8cd1a0000">   <qn:message>custom</qn:message> </qn:querynotification> 

i using xmlreader in following way:

using (xmlreader xmlreader = xmlreader.create(new stringreader(z))) {      xmlreader.read(); } 

when read() executed, following exception:

xmlexception: data @ root level invalid. line 1, position 40.

i don't think likes qn: before each tag. how can setup xmlreader parse document?

your xml has namespace daclared. should consider when parsing nodes. suggest use linq xml:

xnamespace qn = "http://schemas.microsoft.com/sql/notifications/querynotification"; xelement notification = xelement.parse(z); var message = (string)notification.element(qn + "message"); // custom 

Comments