I have read many questions on SO about this topic but I can seem to related to what I am trying to do. Basically I am calling a web service to return information about a Vehicle Identification Number (VIN). It returns an xml stream that looks like this:
<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>
I want to take that data and parse it out for each key/value. This is my code I am using the get the xml data:
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;
}
After I get the xmlDoc I am not sure what to do next.
Thanks for your kind assistance.
You can parse xml to Dictionary<string, string>
with Linq to Xml:
var xdoc = XDocument.Parse(xml);
var items = xdoc.Descendants("Item")
.ToDictionary(i => (string)i.Attribute("Key"),
i => (string)i.Attribute("Value"));
Usage:
string driveline = items["Driveline"];
Or
foreach(var kvp in items)
// use key value pair
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnodelstTrack = xmldoc.GetElementsByTagName("Item");
foreach (XmlNode NodeObj in xmlnodelstTrack)
{
//Do stuff with NodeObj.OuterXml
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With