我想在這裏完成三兩件事:把XmlNode的查詢,得到的XmlNode QueryId並獲得的價值:的schemaLocation但解析後它們最終爲空。如果我從XML中刪除限定名稱,則C#位可以正常工作。我應該如何重寫我的代碼?
XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="localhost">
<soapenv:Header/>
<soapenv:Body>
<loc:Service>
<!--Optional:-->
<loc:input>?</loc:input>
<Query a:schemaLocation="http://www.xyz.com/xml.xsd" xmlns:payload="http://www.xyz.com" xmlns:a="http://www.w3.org/2001/XMLSchema-instance" xmlns="loc4">
<QueryId>Data</QueryId>
</Query>
</loc:Service>
</soapenv:Body>
</soapenv:Envelope>
C#:
private NamespaceManager nsmgr;
private XmlDocument doc = new XmlDocument();
private Stream receiveStream = new Stream(HttpContext.Current.Request.InputStream);
private XmlNode Query, QueryId;
using (this.receiveStream){
using (StreamReader readStream = new StreamReader(this.receiveStream, Encoding.UTF8)){
doc.Load(readStream);
this.nsmgr = new XmlNamespaceManager(this.doc.NameTable);
this.nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
this.nsmgr.AddNamespace("loc", "http://localhost");
this.nsmgr.AddNamespace("schemaLocation", "http://www.xyz.com/xml.xsd");
this.nsmgr.AddNamespace("payload", "http://www.xyz.com");
this.nsmgr.AddNamespace("a", "http://www.w3.org/2001/XMLSchema-instance");
this.nsmgr.AddNamespace("x", this.doc.DocumentElement.NamespaceURI);
this.Query = doc.SelectSingleNode("//x:Query", this.nsmgr);
this.QueryId= doc.SelectSingleNode("//x:QueryId", this.nsmgr);
}
}
XML文檔包含接收到的所有必要信息,但上面的代碼不會將值「Query」和「QueryId」解析爲其尊崇的XmlNode。 – tomtomssi