2013-08-07 33 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黄芪多糖通过的建议。