2012-08-30 13 views
7
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Head> 
    <h:talkId s:mustknow="1" xmlns:h="urn:schemas-test:testgate:hotel:2012-06"> 
     sfasfasfasfsfsf</h:talkId> 
    </s:Head> 
    <s:Body> 
    <bookHotelResponse xmlns="urn:schemas-test:testgate:hotel:2012-06" xmlns:d="http://someURL" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <d:bookingReference>123456</d:bookingReference> 
     <d:bookingStatus>successful</d:bookingStatus> 
     <d:price xmlns:p="moreURL"> 
     <d:total>105</d:total> 
     </d:price> 
    </bookHotelResponse> 
    </s:Body> 
</s:Envelope> 

मैं ऊपर साबुन संदेश XmlDocument का उपयोग कर सी # को पढ़ने के लिए कोशिश कर रहा हूँ:का उपयोग कर पढ़ें साबुन संदेश सी #

XmlDocument document = new XmlDocument(); 
document.LoadXml(soapmessage); //loading soap message as string 
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 

manager.AddNamespace("d", "http://someURL"); 

XmlNodeList xnList = document.SelectNodes("//bookHotelResponse", manager); 
int nodes = xnList.Count; 

foreach (XmlNode xn in xnList) 
{ 
    Status = xn["d:bookingStatus"].InnerText; 
} 

गिनती हमेशा शून्य है और यह bookingstatus मूल्यों को पढ़ने नहीं है।

+0

कृपया स्वरूपण को ठीक करें, खासकर एक्सएमएल संदेश (जिसमें से अधिकांश वर्तमान में गायब है) को ठीक करें। –

+0

एक्सएमएल अमान्य है ... बी एक अपरिभाषित नेमस्पेस है .. यह डी – Anirudha

+1

@ हरिरुधा अर्थ है, बी: कुल होना चाहिए डी: कुल xml नमूना – dthorpe

उत्तर

14

BookHotelResponse नाम स्थान urn:schemas-test:testgate:hotel:2012-06 (डिफ़ॉल्ट में नाम स्थान में है नमूना एक्सएमएल) तो आपको अपने प्रश्नों में उस नामस्थान को प्रदान करने की आवश्यकता है:

XmlDocument document = new XmlDocument(); 
document.LoadXml(soapmessage); //loading soap message as string 
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 

manager.AddNamespace("d", "http://someURL"); 
manager.AddNamespace("bhr", "urn:schemas-test:testgate:hotel:2012-06"); 

XmlNodeList xnList = document.SelectNodes("//bhr:bookHotelResponse", manager); 
int nodes = xnList.Count; 

foreach (XmlNode xn in xnList) 
{ 
    Status = xn["d:bookingStatus"].InnerText; 
} 
+1

काम करता है जब मैंने 'बी:' नेमस्पेस आईडी टाइपो को बदल दिया और से को आपके उदाहरण xml में बदल दिया। क्या आप सुनिश्चित हैं कि आपके साबुन में एक्सएमएल आपके उदाहरण में प्रारूप से मेल खाता है? – dkackman

+0

नमस्ते धन्यवाद, यह काम करता है, क्षमा करें टाइपो गलती है –

3

उपयोग LINQ2XML

bookingStatus पढ़ने के लिए, इस

XElement doc = XElement.Load("yourStream.xml"); 
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s 
XNamespace bhr="urn:schemas-test:testgate:hotel:2012-06";//bookHotelResponse namespace 
XNamespace d="http://someURL";//d namespace 

foreach (var itm in doc.Descendants(s + "Body").Descendants(bhr+"bookHotelResponse")) 
{ 
itm.Element(d+"bookingStatus").Value;//your bookingStatus value 
} 

LINQ2XML शांत हालांकि है .... :)

+0

आपको भी धन्यवाद यह विधि –

1

पहले तुम तो फिर तुम साबुन अनुरोध के शरीर निकालने और एक वस्तु में अनुरोध स्ट्रिंग deseralize को GetElementsByTagName उपयोग कर सकते हैं

public class bookHotelResponse { 
     public int bookingReference { get; set; } 
     public int bookingStatus { get; set; } 
    } 

में एक वर्ग एक्सएमएल मूल्यों deseralize को बनाना चाहते हैं।

private static T DeserializeInnerSoapObject<T>(string soapResponse) 
    { 
     XmlDocument xmlDocument = new XmlDocument(); 
     xmlDocument.LoadXml(soapResponse); 

     var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0]; 
     string innerObject = soapBody.InnerXml; 

     XmlSerializer deserializer = new XmlSerializer(typeof(T)); 

     using (StringReader reader = new StringReader(innerObject)) 
     { 
      return (T)deserializer.Deserialize(reader); 
     } 
    } 
संबंधित मुद्दे