2017-12-18 5 views
-3

に私は、Webサービスからの応答を持っており、私のコードは読む要素は、XMLからのC#

using (WebResponse response2 = request.GetResponse()) 
{ 
    using (StreamReader rd = new StreamReader(response2.GetResponseStream())) 
    { 
     string soapResult = rd.ReadToEnd();     
    } 
} 

を次のようになります。今、私は、文字列soapResultに私の完全な応答を持っています。

soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
    <GetResponse xmlns="http://ws.design.americaneagle.com"> 
    <GetResult> 
     <Distance>0</Distance> 
     <ID>100</ID> 
     <Name>Wisconsin</Name> 
     <Code>WI</Code> 
     <Address1>202 Las COlinas</Address1> 
    </GetResult> 
    </GetResponse> 
</soap:Body> 
</soap:Envelope> 

私は上記のXMLからのID、名前、およびアドレス1を読みたい:

私のXMLは次のようになります。

これを行うには?私はC#でXMLに新しいです。

助けが必要ですか?前もって感謝します。

+5

LINQ to XMLについて学ぶ。 – SLaks

+0

LINQは使用できません。純粋なXML技術が必要です。 – Santosh

+0

該当する場合は、WebサービスプロバイダがXSDを指定しているはずです。 – karritos

答えて

1

xml linqを使用しています。テストのために私は(代わりにWebResponseの)ファイルから読んでいるので、あなたが行くためにわずかな変更を加える必要が戻ってあなたの元のコードに

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      using (StreamReader rd = new StreamReader(FILENAME)) 
      { 
       XDocument doc = XDocument.Load(rd); 

       XElement response = doc.Descendants().Where(x => x.Name.LocalName == "GetResponse").FirstOrDefault(); 
       XNamespace ns = response.GetDefaultNamespace(); 

       var data = response.Descendants(ns + "GetResult").Select(x => new { 
        distance = (int)x.Element(ns + "Distance"), 
        id = (int)x.Element(ns + "ID"), 
        name = (string)x.Element(ns + "Name"), 
        code = (string)x.Element(ns + "Code"), 
        addresss = (string)x.Element(ns + "Address1") 
       }).FirstOrDefault(); 
      } 
     } 
    } 
} 
0

のXmlDocumentを使用してください。ルートステートメントを使用する。それは非常に..それについて読むかXPathを使用することです。それについても読んでください!

0

私はXML to SharpであなたのXMLを貼り付け、これだ:そして、あなたはそれをデシリアライズする必要が

  using System; 
      using System.Xml.Serialization; 
      using System.Collections.Generic; 

      namespace Xml2CSharp 
      { 
       [XmlRoot(ElementName = "GetResult", Namespace = "http://ws.design.americaneagle.com")] 
       public class GetResult 
       { 
        [XmlElement(ElementName = "Distance", Namespace = "http://ws.design.americaneagle.com")] 
        public string Distance { get; set; } 

        [XmlElement(ElementName = "ID", Namespace = "http://ws.design.americaneagle.com")] 
        public string ID { get; set; } 

        [XmlElement(ElementName = "Name", Namespace = "http://ws.design.americaneagle.com")] 
        public string Name { get; set; } 

        [XmlElement(ElementName = "Code", Namespace = "http://ws.design.americaneagle.com")] 
        public string Code { get; set; } 

        [XmlElement(ElementName = "Address1", Namespace = "http://ws.design.americaneagle.com")] 
        public string Address1 { get; set; } 
       } 

       [XmlRoot(ElementName = "GetResponse", Namespace = "http://ws.design.americaneagle.com")] 
       public class GetResponse 
       { 
        [XmlElement(ElementName = "GetResult", Namespace = "http://ws.design.americaneagle.com")] 
        public GetResult GetResult { get; set; } 

        [XmlAttribute(AttributeName = "xmlns")] 
        public string Xmlns { get; set; } 
       } 

       [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
       public class Body 
       { 
        [XmlElement(ElementName = "GetResponse", Namespace = "http://ws.design.americaneagle.com")] 
        public GetResponse GetResponse { get; set; } 
       } 

       [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
       public class Envelope 
       { 
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
        public Body Body { get; set; } 

        [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")] 
        public string Soap { get; set; } 

        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")] 
        public string Xsi { get; set; } 

        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")] 
        public string Xsd { get; set; } 
       } 
      } 

を。

  var serializer = new XmlSerializer(typeof(Envelope)); 
      Envelope result; 

      using (TextReader reader = new StringReader(xml)) 
      { 
       result = (Envelope)serializer.Deserialize(reader); 
      }