获取 NodeList XML 中的节点



好的,所以我没有使用XML,我正在尝试从XML Rest响应中检索某些标签之间的值。 XML 响应如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<root version="1.1" result="Success">
<row>
<NewUsed>used</NewUsed>
<Make>TOYOTA</Make>
<Model>HILUX</Model>
<Derivative>3.0 D-4D Double Cab Raider</Derivative>
<MMCode>60036426</MMCode>
<RegNumber>NP191382</RegNumber>
<Year>2014</Year>
<Mileage>154887.0</Mileage>
<Price>299950.0</Price>
<Media>
<Thumb>ccf1474a-79ed-48c4-9913-b390bc06fddc</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/ccf1474a-79ed-48c4-9913-b390bc06fddc</Pic>
<Thumb>f23359cd-420a-46a6-aedf-46bf574e83f3</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/f23359cd-420a-46a6-aedf-46bf574e83f3</Pic>
<Thumb>d300a13e-47a7-446a-8139-7c72d67c423c</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/d300a13e-47a7-446a-8139-7c72d67c423c</Pic>
<Thumb>a098f3eb-c0de-49e5-b349-a9da58035d09</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/a098f3eb-c0de-49e5-b349-a9da58035d09</Pic>
<Thumb>4551cb8e-7ef2-4307-9e03-985dc4c7ae6b</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/4551cb8e-7ef2-4307-9e03-985dc4c7ae6b</Pic>
<Thumb>3f9e73df-ade1-49a9-920d-4122f0439f3a</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/3f9e73df-ade1-49a9-920d-4122f0439f3a</Pic>
<Thumb>7c76c64b-9ead-4712-a57c-2bee30f1e6e2</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/7c76c64b-9ead-4712-a57c-2bee30f1e6e2</Pic>
<Thumb>4055da15-1ccc-40e1-8e13-32dade2aafe5</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/4055da15-1ccc-40e1-8e13-32dade2aafe5</Pic>
<Thumb>e5a6b77b-dade-4c2f-800c-77988a3d0a83</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/e5a6b77b-dade-4c2f-800c-77988a3d0a83</Pic>
</Media>
<Colour>White</Colour>
<AdvertId>b3beab43909043d6a8c086cd223ffd3b</AdvertId>
<StockNumber>13302</StockNumber>
<AdditionalText>Great condition with spare keys included</AdditionalText>
<FuelType>Diesel</FuelType>
<TransmissionType>Manual</TransmissionType>
<TransmissionDriveType>4x2</TransmissionDriveType>
<Doors>4</Doors>
<BodyType>Double Cab</BodyType>
</row>
<row>
<NewUsed>used</NewUsed>
<Make>VOLKSWAGEN</Make>
<Model>GOLF</Model>
<Derivative>GTI</Derivative>
<MMCode>64045900</MMCode>
<RegNumber>YRY546GP</RegNumber>
<Year>2009</Year>
<Mileage>116000.0</Mileage>
<Price>189950.0</Price>
<Media>
<Thumb>b554d5da-d0cd-4c80-bc57-d75c7bfaf782</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/b554d5da-d0cd-4c80-bc57-d75c7bfaf782</Pic>
<Thumb>73064b26-4322-4de9-b78b-863296f9688a</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/73064b26-4322-4de9-b78b-863296f9688a</Pic>
<Thumb>7d9e2c73-535a-4cde-bb5b-d04af6fb1289</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/7d9e2c73-535a-4cde-bb5b-d04af6fb1289</Pic>
<Thumb>51144896-3837-4b70-b767-c34021af9d07</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/51144896-3837-4b70-b767-c34021af9d07</Pic>
<Thumb>5915d1d5-8e84-4cbc-b7cf-d2131f8fb880</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/5915d1d5-8e84-4cbc-b7cf-d2131f8fb880</Pic>
<Thumb>e052992a-21bc-4473-ab5d-ecae8f50c628</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/e052992a-21bc-4473-ab5d-ecae8f50c628</Pic>
<Thumb>cebea6d3-bb2a-4389-b132-3cb9a70d5da5</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/cebea6d3-bb2a-4389-b132-3cb9a70d5da5</Pic>
<Thumb>039034b9-db10-4c27-857d-0e071212949d</Thumb>
<Pic>https://i.autotrader.co.za/merlin-image-server/view/039034b9-db10-4c27-857d-0e071212949d</Pic>
</Media>
<Colour>Silver</Colour>
<AdvertId>4b2fc17fb8574b70a030db286c76aba0</AdvertId>
<StockNumber>13498</StockNumber>
<AdditionalText>GOOD CONDITION WITH FULL SERVICE HISTORY AND SPARE KEY INCLUDED</AdditionalText>
<FuelType>Petrol</FuelType>
<TransmissionType>Manual</TransmissionType>
<TransmissionDriveType>FWD</TransmissionDriveType>
<Doors>5</Doors>
<BodyType>Hatchback</BodyType>
</row>

我能够很好地检索所有标签,除了"媒体"标签中的"图片"标签。 我的代码如下:

private XmlDocument GetRootLevelServiceDocument(string serviceEndPoint, string oAuthToken)
{
HttpWebRequest request = CreateHttpRequest(serviceEndPoint,
oAuthToken);
XmlDocument xmlDoc = new XmlDocument();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
string r = reader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(r);
XmlNodeList xnList = doc.SelectNodes("/root/row");
foreach (XmlNode xn in xnList)
{
string img = "";
double install = 3000;
divVehicles.InnerHtml += "<div class='inventory margin-bottom-20 clearfix scroll_effect fadeIn'><a class='inventory' href='inventory-listing.html'>";
divVehicles.InnerHtml += "<div class='title'>"+ xn["Year"].InnerText +" "+ xn["Make"].InnerText  +" " + xn["Model"].InnerText + " " + xn["Derivative"].InnerText+ "</div>";
divVehicles.InnerHtml += "<img src='"+ img + "' class='preview' alt='Image'>";
divVehicles.InnerHtml += "<table class='options-primary'><tr><td class='option primary'>Mileage:</td>";
divVehicles.InnerHtml += "<td class='spec'>"+ xn["Mileage"].InnerText + "km</td>";
divVehicles.InnerHtml += "</tr><tr><td class='option primary'>Colour:</td><td class='spec'>"+ xn["Colour"].InnerText + "</td>";
divVehicles.InnerHtml += "</tr><tr><td class='option primary'>Transmission:</td><td class='spec'>" + xn["TransmissionType"].InnerText + "</td></tr><tr>";
divVehicles.InnerHtml += "<td class='option primary'>Fuel:</td><td class='spec'>" + xn["FuelType"].InnerText + "</td></tr></table>";
divVehicles.InnerHtml += "<table class='options-secondary'><tr><td class='option secondary'>YOU GET:</td></tr><tr><td class='spec'>- A FREE full tank of Fuel</td>";
divVehicles.InnerHtml += "</tr><tr><td class='spec'>- ...</td></tr><tr><td class='spec'>- ...</td></tr></table>";
divVehicles.InnerHtml += "<img src='images/firstcar.jpg' alt='image' class='carfax' />";
divVehicles.InnerHtml += "<div class='view-video gradient_button' data-youtube-id='3oh7PBc33dk'><i class='fa fa-video-camera'></i>Finance Now</div><div class='price'><b>Full Price: R" + xn["Price"].InnerText + "</b><br>";
divVehicles.InnerHtml += "<div class='figure'>R"+install+".00 pm<br></div><div class='tax'>Pay Less? Ask us how!</div></div>";
divVehicles.InnerHtml += "<div class='view-details gradient_button'><i class='fa fa-plus-circle'></i> View Details </div><div class='clearfix'></div> </a></div>";
}
xmlDoc = doc;
//  test.InnerHtml =  Server.HtmlEncode(doc.InnerXml);
}
}
}
return xmlDoc;
}

访问"标签的正确方法是什么?原谅我的无知。 提前谢谢。

您可以使用 XPath 来选择所需的元素。下面是一个使用 XElement 的简单示例。

更新:添加了按行选择

XElement xmlData = XElement.Load("media.xml");
var mediaNodes = xmlData.XPathSelectElements(@"/row/Media");
foreach (var mediaElement in mediaNodes)
{
var picElements = mediaElement.XPathSelectElements("Pic");
foreach (var picElement in picElements)
{
var picValue = picElement.Value;
}
}

尝试使用 xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<Car> cars = doc.Descendants("row").Select(x => new Car() {
newUser = (string)x.Element("NewUsed"),
make = (string)x.Element("Make"),
model = (string)x.Element("Model"),
derivative = (string)x.Element("Derivative"),
code = (string)x.Element("MMCode"),
registrationNumber = (string)x.Element("RegNumber"),
year = (int)x.Element("Year"),
mileage = (decimal)x.Element("Mileage"),
price = (decimal)x.Element("Price"),
pictures = x.Element("Media").Elements().Select((y,i) => new { value = (string)y, i = i})
.GroupBy(y => y.i / 2)
.GroupBy(y => (string)y.FirstOrDefault().value, z => (string)z.Last().value)
.ToDictionary(y => y.Key, z => z.FirstOrDefault())
}).ToList();

}
}
public class Car
{
public string newUser { get; set; }
public string make { get; set; }
public string model { get; set; }
public string derivative { get; set; }
public string code { get; set; }
public string registrationNumber { get; set; }
public int year { get; set; }
public decimal mileage { get; set; }
public decimal price { get; set; }
public Dictionary<string, string> pictures { get; set; }
}
}

相关内容

  • 没有找到相关文章

最新更新