1
我一直無法解析其中包含多個命名空間下面的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
我嘗試過,但似乎沒有工作。 – codelearner
@codelearner對不起,我忽略了'GetMailTipsResponse'引入了另一個沒有前綴的名字空間。修改了上面的代碼。 – LocEngineer
我也嘗試過,並且似乎沒有工作。 – codelearner