解析XML与C#多个命名空间
问题描述:
我一直无法解析其中包含多个命名空间下面的XML:解析XML与C#多个命名空间
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="14" MinorVersion="3" MajorBuildNumber="352" MinorBuildNumber="0" Version="Exchange2010_SP2" />
</s:Header>
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<GetMailTipsResponse xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" ResponseClass="Success">
<ResponseCode>NoError</ResponseCode>
<ResponseMessages>
<MailTipsResponseMessageType ResponseClass="Success">
<ResponseCode>NoError</ResponseCode>
<m:MailTips xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
<t:RecipientAddress xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<t:Name />
<t:EmailAddress>[email protected]</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
</t:RecipientAddress>
<t:PendingMailTips xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" />
<t:OutOfOffice xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<t:ReplyBody>
<t:Message />
</t:ReplyBody>
</t:OutOfOffice>
</m:MailTips>
</MailTipsResponseMessageType>
</ResponseMessages>
</GetMailTipsResponse>
</s:Body>
</s:Envelope>
我试着下面的代码,但你可以看到用肥皂命名空间的作品的第一个节点不错,但此后我无法找回,我需要它是节点的节点信息 -
/s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType/m:MailTips/t:RecipientAddress/t:Message
这里是我试过的代码:
string getXmlInfo (string resultXml)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resultXml);
XmlNamespaceManager soapNsManager = new XmlNamespaceManager(xmlDoc.NameTable);
soapNsManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNode xmlNode = xmlDoc.SelectSingleNode("//s:Envelope/s:Body", soapNsManager); //works fine - the node now contains the Xml starting with the node
XmlNode xmlNode1 = xmlDoc.SelectSingleNode("//s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType", soapNsManager); //returns NULL
XmlNode innerNode1 = xmlNode.SelectSingleNode("//GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType"); // returns NULL
XmlNode innerNode2 = xmlNode.SelectSingleNode("//GetMailTipsResponse", soapNsManager); //returns NULL
// the next line throws an exception
//XmlNode messageNode = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType/m:MailTips/t:RecipientAddress/t:Message", manager);
}
这是基于从@LocEngineer响应我的尝试:
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
manager.AddNamespace("blank", "http://schemas.microsoft.com/exchange/services/2006/types");
manager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/types");
manager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");
XmlNode messageNode = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/blank:GetMailTipsResponse/blank:ResponseMessages/blank:MailTipsResponseMessageType/m:MailTips/t:OutOfOffice/t:ReplyBody/t:Message", manager);
的messageNode显示为NULL
答
您还需要一个XmlNamespaceManager的前缀推出的前缀命名空间少在GetMailTipsResponse节点中,它包含XML中没有前缀的节点:
GetMailTipsResponse xmlns =“http:// schemas。 microsoft.com/exchange/services/2006/messages”
,然后用它在你的XPath适当,使用‘M’为前缀的前缀少节点:
XmlNamespaceManager soapNsManager = new XmlNamespaceManager(xmlDoc.NameTable);
soapNsManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
soapNsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
XmlNode xmlNode1 = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/m:GetMailTipsResponse/m:ResponseMessages/m:MailTipsResponseMessageType", soapNsManager);
我尝试过,但似乎没有工作。 – codelearner
@codelearner对不起,我忽略了'GetMailTipsResponse'引入了另一个没有前缀的名字空间。修改了上面的代码。 – LocEngineer
我也尝试过,并且似乎没有工作。 – codelearner