[英]Select single xml node w/ namespaces with XPath Csharp
为什么我的xmlNode节点总是以null结尾? 我放入了它要求的名称空间。 以下是我已加载的xml。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<soapenv:Body>
<queryResponse>
<result>
<done>true</done>
<queryLocator xsi:nil="true"></queryLocator>
<records xsi:type="sf:Opportunity">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Account xsi:type="sf:Account">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Account_ID__c>AccountIdXXXX</sf:Account_ID__c>
<sf:Name>The Account Name</sf:Name>
</sf:Account>
<sf:OpportunityLineItems>
<done>true</done>
<queryLocator xsi:nil="true"></queryLocator>
<records xsi:type="sf:OpportunityLineItem">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Brand_Desc__c>Another Description</sf:Brand_Desc__c>
<sf:PricebookEntry xsi:type="sf:PricebookEntry">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Product2 xsi:type="sf:Product2">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Description>The Product Description</sf:Description>
</sf:Product2>
</sf:PricebookEntry>
<sf:Product_Group_for_Storage_Sys__c>Some Descriptive Text</sf:Product_Group_for_Storage_Sys__c>
<sf:Product_Type_Formula__c>Enterprise</sf:Product_Type_Formula__c>
<sf:Product_Type_Text__c>Enterprise</sf:Product_Type_Text__c>
<sf:Product_Type__c>Enterprise</sf:Product_Type__c>
<sf:Product_code_for_analytics__c>Code Here</sf:Product_code_for_analytics__c>
</records>
<size>1</size>
</sf:OpportunityLineItems>
<sf:PartnerAccount xsi:type="sf:Account">
<sf:Id xsi:nil="true"></sf:Id>
<sf:Account_ID__c>IDXXXX</sf:Account_ID__c>
</sf:PartnerAccount>
<sf:Partner_Account_Name__c>The Partner Name</sf:Partner_Account_Name__c>
</records>
<size>1</size>
</result>
</queryResponse>
</soapenv:Body>
</soapenv:Envelope>
xmlSoapRequest.LoadXml(strResultString);
XmlNamespaceManager man = new XmlNamespaceManager(xmlSoapRequest.NameTable);
man.AddNamespace("sf", "urn:sobject.enterprise.soap.sforce.com");
man.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNode nodes = xmlSoapRequest.SelectSingleNode("/soapenv:Envelope/soapenv:Body/queryResponse/result/records/sf:Account/sf:Name", man);
看来我做的一切正确。 我给它了织补名称空间。 但是节点总是以null结尾。
一个更简单的XPath语句最终起作用了。
XmlNode nodes = xmlSoapRequest.SelectSingleNode("//sf:Account/sf:Name", man);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.