-4

I'm sure this is very simple..

I have this as a string:

<OnTheRoadQuote xmlns:i="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://schemas.datacontract.org/2004/07/OTRAPI.Services.Models">
<BasicPrice>15595.8333</BasicPrice>
<CO2>93</CO2>
<Dealer>Audi</Dealer>
<DeliveryCost>524.9900</DeliveryCost>
<DiscountPrice>14348.166636</DiscountPrice>
<DiscountSum>1247.666664</DiscountSum>
<Discounts>
<Discount>
<DiscountApplication>Percentage</DiscountApplication>
<DiscountDescription>Dealer Discount on Vehicle and Options %</DiscountDescription>
<DiscountID>Discount1</DiscountID>
<DiscountType>VehicleAndOptions</DiscountType>
<DiscountValue>8</DiscountValue>
</Discount>
</Discounts>
<OTR>17902.7879632</OTR>
</OnTheRoadQuote>

How do I read the value of the OTR node?

I've got an XmlReader but not sure how to use it.

Thanks

Ben Durkin
  • 429
  • 1
  • 6
  • 20
  • possible duplicate of [C# - Reading data from XML](http://stackoverflow.com/questions/7119806/c-sharp-reading-data-from-xml) – Marc B Jun 09 '15 at 14:38
  • 1
    Why have you not googled this? There are countless articles on how to read data from XML in .NET – BenCr Jun 09 '15 at 14:39
  • In fairness, the related questions focus on using `XmlDocument` and/or `XDocument`. He's asking specifically how to load a string into an `XmlReader` – Dan Field Jun 09 '15 at 14:45

2 Answers2

2

Using XmlReader, and fixing a typo in your root element (missing space before the second xmlns):

string xml = @"<OnTheRoadQuote xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/OTRAPI.Services.Models"">
  <BasicPrice>15595.8333</BasicPrice>
  <CO2>93</CO2>
  <Dealer>Audi</Dealer>
  <DeliveryCost>524.9900</DeliveryCost>
  <DiscountPrice>14348.166636</DiscountPrice>
  <DiscountSum>1247.666664</DiscountSum>
  <Discounts>
    <Discount>
        <DiscountApplication>Percentage</DiscountApplication>
        <DiscountDescription>Dealer Discount on Vehicle and Options %</DiscountDescription>
        <DiscountID>Discount1</DiscountID>
        <DiscountType>VehicleAndOptions</DiscountType>
        <DiscountValue>8</DiscountValue>
    </Discount>
  </Discounts>
  <OTR>17902.7879632</OTR>
</OnTheRoadQuote>";

string otrValue = "";
using (XmlReader reader = XmlReader.Create(new StringReader(xml))) // use a StringReader to load the XML string into an XmlReader
{
   reader.ReadToFollowing("OTR"); // move the reader to OTR
   reader.ReadStartElement(); // consume the start element
   otrValue = reader.Value; // store the value in the otrValue string.
}

Keep in mind that XmlReader is forward only, meaning that you can't navigate it backwards through the XML data to read, for example, Discounts, once you've pushed it to the OTR node. If you want to do that, you should look into using XmlDocument or (preferably) XDocument. However, if all you need to do is get the OTR value, this should be the most efficient (time and space) way of doing so.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
0

With less code, you can use LINQ to XML and load an XElement with the xml or a file. There are also other options.

XElement element = XElement.Load(....);
var node = element.Element("OTR");

https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.load%28v=vs.110%29.aspx

William Xifaras
  • 5,212
  • 2
  • 19
  • 21