2013-01-20 108 views
1

我已經閱讀了許多有關此主題的問題,但似乎與我正在嘗試做的事情有關。基本上,我打電話給Web服務以返回有關車輛識別號碼(VIN)的信息。它返回一個看起來像這樣的XML流:解析由Web服務響應返回的xml

<VINquery Version="1.0.0" Date="5/25/2003"> 
    <VIN Number="1HGES15501Lxxxxxx" Status="SUCCESS"> 
    <Vehicle VINquery_Vehicle_ID="23261" Model_Year="2001" Make="Honda" Model="Civic" Trim_Level="LX Sedan"> 
     <Item Key="VINquery Vehicle ID" Value="23261" Unit=""/> 
     <Item Key="Model Year" Value="2001" Unit=""/> 
     <Item Key="Make" Value="Honda" Unit=""/> 
     <Item Key="Model" Value="Civic" Unit=""/> 
     <Item Key="Trim Level" Value="LX Sedan" Unit=""/> 
     <Item Key="Manufactured in" Value="UNITED STATES" Unit=""/> 
     <Item Key="Production Seq. Number" Value="xxxxxx" Unit=""/> 
     <Item Key="Body Style" Value="SEDAN 4-DR" Unit=""/> 
     <Item Key="Engine Type" Value="1.7L L4 SOHC 16V" Unit=""/> 
     <Item Key="Transmission-short" Value="5M OD" Unit=""/> 
     <Item Key="Transmission-long" Value="5-Speed Manual Overdrive" Unit=""/> 
     <Item Key="Driveline" Value="FWD" Unit=""/> 
     <Item Key="Tank" Value="13.20" Unit="gallon"/> 
     <Item Key="Fuel Economy-city" Value="30 - 32" Unit="miles/gallon"/> 
     <Item Key="Fuel Economy-highway" Value="38 - 39" Unit="miles/gallon"/> 
     <Item Key="Anti-Brake System" Value="Non-ABS" Unit=""/> 
     <Item Key="Steering Type" Value="R&P" Unit=""/> 
     <Item Key="Standard Seating" Value="5" Unit=""/> 
     <Item Key="Optional Seating" Value="No data" Unit=""/> 
     <Item Key="Length" Value="174.60" Unit="in."/> 
     <Item Key="Width" Value="67.50" Unit="in."/> 
     <Item Key="Height" Value="56.70" Unit="in."/> 
    </Vehicle> 
    </VIN> 
</VINquery> 

我想利用這些數據和分析它爲每個鍵/值。 這是我的代碼,我使用的獲取XML數據:

private string baseURL = "http://ws.vinquery.com/restxml.aspx?accessCode=xxxxxx&vin=xxxxxxxxx"; 
private string reportType = "&reportType=2"; 

public XmlDocument ExplodeVIN(string strVIN) 
{ 
    DataTable dt = new DataTable(); 

    string url = baseURL + strVIN + reportType; 

    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; 

    XmlDocument xmlDoc = new XmlDocument(); 
    using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) 
    { 
    xmlDoc.Load(resp.GetResponseStream()); 
    } 
    return xmlDoc; 
} 

後,我得到了xmlDoc中,我不知道下一步該怎麼做。

感謝您的協助。

回答

5

您可以使用LINQ to XML解析到Dictionary<string, string>到XML:

var xdoc = XDocument.Parse(xml); 
var items = xdoc.Descendants("Item") 
       .ToDictionary(i => (string)i.Attribute("Key"), 
           i => (string)i.Attribute("Value")); 

用法:

string driveline = items["Driveline"]; 

或者

foreach(var kvp in items) 
    // use key value pair 
+0

我不明白你的行:var xdoc = XDocument.Parse(xml)。解析方法查找字符串。字符串xml從哪裏來? – Ryan

+0

@Ryan應該是服務響應字符串。如果你有響應流,然後使用'XDocument.Load(stream)'而不是 –

+0

這似乎是在關閉... var items = xdoc.Descendants(「Item」)。ToDictionary(i =>(string)i.Element (「Key」), i =>(string)i.Element(「Value」));導致一個參數爲空的異常,我不知道爲什麼 – Ryan

2
XmlDocument xmldoc = new XmlDocument(); 
XmlNodeList xmlnodelstTrack = xmldoc.GetElementsByTagName("Item"); 
foreach (XmlNode NodeObj in xmlnodelstTrack) 
{ 
    //Do stuff with NodeObj.OuterXml 

} 
+0

我也喜歡這個解決方案。在我的xml示例中,我是否會通過「Vehicle」節點來獲取Item節點鍵/值?或者我應該通過「項目」來獲取鍵/值循環? – Ryan

+0

@Ryan Yep,通過項目 –

1

還有另一種方式,我通常使用處理xml。

第1步:複製您的xml字符串並粘貼爲類。

Paste XML As Classes

當你貼吧,該代碼會自動生成:

 /// <remarks/> 
     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
     [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
     public partial class VINquery 
     { 

      private VINqueryVIN vINField; 

      private string versionField; 

      private string dateField; 

      /// <remarks/> 
      public VINqueryVIN VIN 
      { 
       get 
       { 
        return this.vINField; 
       } 
       set 
       { 
        this.vINField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Version 
      { 
       get 
       { 
        return this.versionField; 
       } 
       set 
       { 
        this.versionField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Date 
      { 
       get 
       { 
        return this.dateField; 
       } 
       set 
       { 
        this.dateField = value; 
       } 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
     public partial class VINqueryVIN 
     { 

      private VINqueryVINVehicle vehicleField; 

      private string numberField; 

      private string statusField; 

      /// <remarks/> 
      public VINqueryVINVehicle Vehicle 
      { 
       get 
       { 
        return this.vehicleField; 
       } 
       set 
       { 
        this.vehicleField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Number 
      { 
       get 
       { 
        return this.numberField; 
       } 
       set 
       { 
        this.numberField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Status 
      { 
       get 
       { 
        return this.statusField; 
       } 
       set 
       { 
        this.statusField = value; 
       } 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
     public partial class VINqueryVINVehicle 
     { 

      private VINqueryVINVehicleItem[] itemField; 

      private ushort vINquery_Vehicle_IDField; 

      private ushort model_YearField; 

      private string makeField; 

      private string modelField; 

      private string trim_LevelField; 

      /// <remarks/> 
      [System.Xml.Serialization.XmlElementAttribute("Item")] 
      public VINqueryVINVehicleItem[] Item 
      { 
       get 
       { 
        return this.itemField; 
       } 
       set 
       { 
        this.itemField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public ushort VINquery_Vehicle_ID 
      { 
       get 
       { 
        return this.vINquery_Vehicle_IDField; 
       } 
       set 
       { 
        this.vINquery_Vehicle_IDField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public ushort Model_Year 
      { 
       get 
       { 
        return this.model_YearField; 
       } 
       set 
       { 
        this.model_YearField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Make 
      { 
       get 
       { 
        return this.makeField; 
       } 
       set 
       { 
        this.makeField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Model 
      { 
       get 
       { 
        return this.modelField; 
       } 
       set 
       { 
        this.modelField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Trim_Level 
      { 
       get 
       { 
        return this.trim_LevelField; 
       } 
       set 
       { 
        this.trim_LevelField = value; 
       } 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
     public partial class VINqueryVINVehicleItem 
     { 

      private string keyField; 

      private string valueField; 

      private string unitField; 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Key 
      { 
       get 
       { 
        return this.keyField; 
       } 
       set 
       { 
        this.keyField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Value 
      { 
       get 
       { 
        return this.valueField; 
       } 
       set 
       { 
        this.valueField = value; 
       } 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlAttributeAttribute()] 
      public string Unit 
      { 
       get 
       { 
        return this.unitField; 
       } 
       set 
       { 
        this.unitField = value; 
       } 
      } 
     } 

第2步: 反序列化XML字符串,這個生成的類。

public T DeserializeXML<T>(string xmlContent) 
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(T)); 
    MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent)); 
    return (T)serializer.Deserialize(memStream); 
} 

.... 
string resultXml = resp.GetResponseStream(); 
VINquery resultObject = DeserializeXML<VINquery>(resultObject); 

現在,您可以輕鬆操作並過濾結果。

P.S .:您必須刪除xml字符串中的特殊字符(&),因爲它會在您嘗試將其粘貼爲類時導致問題。

+0

使用這種方法的任何提示,當它響應10Mb,即非常長? –