简体   繁体   English

从字符串解析 XML

[英]Parsing XML from string

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?如何读取OTR节点的值?

I've got an XmlReader but not sure how to use it.我有一个 XmlReader 但不知道如何使用它。

Thanks谢谢

Using XmlReader, and fixing a typo in your root element (missing space before the second xmlns ):使用 XmlReader,并修复根元素中的拼写错误(第二个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.请记住, XmlReader 是forward only ,这意味着一旦将其推送到OTR节点,您就无法通过 XML 数据向后导航以读取例如Discounts If you want to do that, you should look into using XmlDocument or (preferably) XDocument .如果你想这样做,你应该考虑使用XmlDocument或(最好) 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.但是,如果您只需要获取 OTR 值,这应该是最有效(时间和空间)的方法。

With less code, you can use LINQ to XML and load an XElement with the xml or a file.使用较少的代码,您可以使用 LINQ to XML 并使用 xml 或文件加载 XElement。 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 https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.load%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM