2016-08-02 7 views
2

Ich brauche einen Wert aus meiner WCF-Nachricht. Meine Nachricht hat den folgenden Wert im Debugger:Verwenden Sie XmlDictionaryReader, um einen Wert aus einer Nachricht zu erhalten

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IBrokerService/SaveAndPrint</Action> 
    </s:Header> 
    <s:Body> 
    <SaveAndPrint xmlns="http://tempuri.org/"> 
     <contract xmlns:d4p1="http://schemas.datacontract.org/2004/07/BrokerService.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <d4p1:ContainerHistoryContracts i:nil="true" /> 
     <!--Lots of nodes removed for brevity--> 
     <d4p1:CurrentBagId>123456</d4p1:CurrentBagId> 
     <!--Lots more nodes removed for brevity--> 
     <d4p1:WorkStation>TheNeededValue</d4p1:WorkStation> 
     </contract> 
    </SaveAndPrint> 
    </s:Body> 
</s:Envelope> 

Aber versuchen Sie, wie ich könnte, ich kann nicht die XmlDictionaryReader verwenden, um den d4p1:WorkStation Wert zu erhalten. Wer weiß, wie man das mit XmlDictionaryReader macht?

HINWEIS: Ich habe versucht, TypedMessageConverter zu verwenden, aber die clasess erzeugt haben nicht das Attribut MessageContract (sie haben DataContract though)

Update: Was ich habe nicht funktioniert. Aber einhüllen wollen Sie es sehen, hier ist sie:

 // Load the message into an xml doc 
     var navigator = buffer.CreateNavigator(); 
     MemoryStream memoryStream = new MemoryStream(); 
     XmlWriter xmlWriter = XmlWriter.Create(memoryStream); 
     navigator.WriteSubtree(xmlWriter); 
     xmlWriter.Flush(); 
     xmlWriter.Close(); 
     memoryStream.Position = 0; 

     XDocument xdoc = XDocument.Load(XmlReader.Create(memoryStream)); 
     var workstationElement = 
      xdoc.Descendants(XName.Get("StringValue", 
       @"/s:Envelope[@xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""]/s:Body/SaveAndPrint[@xmlns=""http://tempuri.org/""]/contract[@xmlns:d4p1=""http://schemas.datacontract.org/2004/07/BrokerService.Contracts""]/d4p1:WorkStation")); 
+0

Wie sieht Ihr XML-Parsing-Code aussehen? – DVK

+0

@DVK - Nicht sicher, wie mein Code funktioniert, aber ich habe es hinzugefügt. – Vaccano

Antwort

0

Try xml linq

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 


namespace ConsoleApplication6 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      //using unique keys 
      Dictionary<string, string> dict1 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements() 
       .GroupBy(x => x.Name.LocalName, y => ((string)y).Trim()) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 
      //when there are duplicate keys 
      Dictionary<string, List<string>> dict2 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements() 
       .GroupBy(x => x.Name.LocalName, y => ((string)y).Trim()) 
       .ToDictionary(x => x.Key, y => y.ToList()); 


     } 
    } 
}