2013-07-15 20 views
0

我試圖解析以下XML字符串:XmlReader無法用<qn:xxxxx>格式解析XML?

<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> 

我以下列方式使用一個XmlReader:

using (XmlReader xmlReader = XmlReader.Create(new StringReader(z))) 
{ 
    xmlReader.Read(); 
} 

當執行閱讀(),我得到以下異常:

XmlException:根級別的數據無效。第1行,第40位。

我不認爲它喜歡每個標籤之前的qn:。我如何設置XmlReader來解析這個文檔?

+1

「z」的值究竟是什麼?它表示錯誤在'位置40',但是在問題中發佈的xml中有40個字符位於命名空間URI的中間。如果我用'var z =「 Random832

+0

嗯...我解析z從Unicode字符串像這樣:string z = Encoding.Unicode.GetString(binaryString);我不知道是否有隱藏的無效字符... – lehn0058

+0

什麼是binaryString?你能打印z的值嗎? – Random832

回答

0

你的xml有命名空間daclared。解析節點時應該考慮它。我建議你使用Linq到XML:

XNamespace qn = "http://schemas.microsoft.com/SQL/Notifications/QueryNotification"; 
XElement notification = XElement.Parse(z); 
var message = (string)notification.Element(qn + "Message"); // Custom