从 C# 中的 xml 文件中选择多个值

Posted

技术标签:

【中文标题】从 C# 中的 xml 文件中选择多个值【英文标题】:select multiple values from xml file in c# 【发布时间】:2012-06-12 23:48:20 【问题描述】:

我正在尝试从 xml 文档中选择三个值。 我想从节点内获取值。 xml 文件中有五个值,我想知道采用它们的最佳方法是什么。 请在下面找到 xml 文件。 提前致谢,拉齐尔

  <?xml version="1.0" encoding="UTF-8"?>
<RatingServiceSelectionResponse>
   <Response>
      <TransactionReference>
         <CustomerContext>Bare Bones Rate Request</CustomerContext>
         <XpciVersion>1.0001</XpciVersion>
      </TransactionReference>
      <ResponseStatusCode>1</ResponseStatusCode>
      <ResponseStatusDescription>Success</ResponseStatusDescription>
   </Response>
   <RatedShipment>
      <Service>
         <Code>03</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>32.44</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>32.44</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery />
      <ScheduledDeliveryTime />
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>32.44</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>32.44</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>24.52</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
   <RatedShipment>
      <Service>
         <Code>12</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>93.32</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>93.32</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery>3</GuaranteedDaysToDelivery>
      <ScheduledDeliveryTime />
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>93.32</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>93.32</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>69.95</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
   <RatedShipment>
      <Service>
         <Code>02</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>148.56</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>148.56</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery>2</GuaranteedDaysToDelivery>
      <ScheduledDeliveryTime />
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>148.56</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>148.56</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>99.21</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
   <RatedShipment>
      <Service>
         <Code>13</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>204.04</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>204.04</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery>1</GuaranteedDaysToDelivery>
      <ScheduledDeliveryTime />
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>204.04</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>204.04</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>129.82</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
   <RatedShipment>
      <Service>
         <Code>14</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>248.35</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>248.35</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery>1</GuaranteedDaysToDelivery>
      <ScheduledDeliveryTime>8:00 A.M.</ScheduledDeliveryTime>
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>248.35</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>248.35</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>248.35</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
   <RatedShipment>
      <Service>
         <Code>01</Code>
      </Service>
      <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
      <BillingWeight>
         <UnitOfMeasurement>
            <Code>LBS</Code>
         </UnitOfMeasurement>
         <Weight>34.0</Weight>
      </BillingWeight>
      <TransportationCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>212.86</MonetaryValue>
      </TransportationCharges>
      <ServiceOptionsCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>0.00</MonetaryValue>
      </ServiceOptionsCharges>
      <TotalCharges>
         <CurrencyCode>USD</CurrencyCode>
         <MonetaryValue>212.86</MonetaryValue>
      </TotalCharges>
      <GuaranteedDaysToDelivery>1</GuaranteedDaysToDelivery>
      <ScheduledDeliveryTime>10:30 A.M.</ScheduledDeliveryTime>
      <RatedPackage>
         <TransportationCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>212.86</MonetaryValue>
         </TransportationCharges>
         <ServiceOptionsCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>0.00</MonetaryValue>
         </ServiceOptionsCharges>
         <TotalCharges>
            <CurrencyCode>USD</CurrencyCode>
            <MonetaryValue>212.86</MonetaryValue>
         </TotalCharges>
         <Weight>13.0</Weight>
         <BillingWeight>
            <UnitOfMeasurement>
               <Code>LBS</Code>
            </UnitOfMeasurement>
            <Weight>34.0</Weight>
         </BillingWeight>
      </RatedPackage>
      <NegotiatedRates>
         <NetSummaryCharges>
            <GrandTotal>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>135.38</MonetaryValue>
            </GrandTotal>
         </NetSummaryCharges>
      </NegotiatedRates>
   </RatedShipment>
</RatingServiceSelectionResponse>

编辑:我需要从 NegotiatedRates 元素中获取 MonetaryValue。 对于那个很抱歉。谢谢

【问题讨论】:

这 3 个值是什么?什么领域? 您有兴趣从哪些节点类型中检索哪些值? 更新了抱歉,谢谢 【参考方案1】:

使用LINQtoXML 处理此问题

string filePath = Server.MapPath(@"../YourFileLocation/somexml.xml");

//you can load the XML from file/stream /string etc...

XElement elem = XElement.Load(filePath);
if (elem != null)

    var items = elem.Descendants("NegotiatedRates").ToList();
    foreach (var singleItem in items)
    
        var mo = singleItem.Descendants("MonetaryValue").SingleOrDefault();
        string monetaryValue = mo.Value;
    

根据您提供的 XML,您将在 items 变量和 foreach 中获得 6 个 items,您将在字符串变量 monetaryValue 中获得 MonetaryValue 元素的值

【讨论】:

以上是关于从 C# 中的 xml 文件中选择多个值的主要内容,如果未能解决你的问题,请参考以下文章

如何从 URL 读取 Windows 应用程序中的大量 xml 文件(从 Windows 应用程序到服务器的多个请求)c#

如何在将 xml 反序列化为 c# 对象时获取单个 xml 元素的多个值?

使用 XSLT 基于 ID 从多个 xPath 中选择 XML 节点

如何从多个 .csv 文件中的命名列中选择唯一值?

如何在 Asp.net MVC C# 中使用 Linq 从多个表中选择具有最大计数值的记录

C# 如何读取具有字符级格式的 Excel 单元格 XML 值? (<si> 中的多个 <t> 元素)