2013-08-07 36 views
0

編輯:獲取XML元素的限定名稱和它的子節點用C#

我想在這裏完成三兩件事:把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); 
    } 
} 
+0

XML文檔包含接收到的所有必要信息,但上面的代碼不會將值「Query」和「QueryId」解析爲其尊崇的XmlNode。 – tomtomssi

回答

0

在這裏你去...

XmlDocument xDoc = new XmlDocument(); 
xDoc.Load("Query.xml"); 

XmlNamespaceManager xnm = new XmlNamespaceManager(xDoc.NameTable); 
xnm.AddNamespace("schemaLocation", "loc"); 
xnm.AddNamespace("payload", "loc2"); 
xnm.AddNamespace("a", "http://www.w3.org/2001/XMLSchema-instance"); 
xnm.AddNamespace("x", xDoc.DocumentElement.NamespaceURI); 

內部文本查詢:

xDoc.SelectNodes("//x:Query", xnm)[0].InnerText 

內部文本對於QueryId:

xDoc.SelectNodes("//x:QueryId", xnm)[0].InnerText 

一個:schemaLocation屬性:

string namespaceURI = xnm.GetNamespacesInScope(XmlNamespaceScope.Local).FirstOrDefault(el => string.Equals(el.Key, "a")).Value; 
var x = xDoc.DocumentElement.Attributes["schemaLocation", namespaceURI].Value; 
+0

節點仍爲空。我修改我的帖子只是抓住節點。任何想法還有什麼問題? – tomtomssi

+0

@tomtomssi - 我試過這段代碼,可以看到它獲取結果。你可以發佈代碼(從你的代碼文件)使用你試圖提取InnerText和屬性值? – Prash

+0

我修改了前面例子中的代碼。我刪除了InnerText位,因爲問題在於節點。我使用上面的代碼嘗試提取節點「Query」和「QueryId」,但它們最終爲空。 – tomtomssi

0

這是我想出了一個簡單的解決方案:

刪除了以下線路:

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("x", "loc4"); 

找到的值:工作的schemaLocation黃芪多糖通過的建議。