1

这是我正在使用的 XML 有多个 LineItemDetails 实例我只发布了其中一个...

<LineItemDetail>
    <DetailNumber>1</DetailNumber> 
    <LineItemNumber>1</LineItemNumber> 
    <BatchSequenceNumber>1</BatchSequenceNumber> 
    <RecordSequenceWithinBatch>1</RecordSequenceWithinBatch> 
    <ChargeAmount Name="GrossBilled">365.380</ChargeAmount> 
    <Tax>
        <TaxType>Tax</TaxType> 
        <TaxAmount Name="Billed">15.630</TaxAmount> 
        <TaxBreakdown>
            <TaxCode>MN</TaxCode> 
            <TaxAmount Name="Billed">15.630</TaxAmount> 
        </TaxBreakdown>
    </Tax>
    <AddOnCharges>
        <AddOnChargeName>ISCAllowed</AddOnChargeName> 
        <AddOnChargePercentage>-9</AddOnChargePercentage> 
        <AddOnChargeAmount>-32.880</AddOnChargeAmount> 
    </AddOnCharges>
    <TotalNetAmount>348.130</TotalNetAmount> 
    <CouponDetails>
        <TicketOrFIMIssuingAirline>160</TicketOrFIMIssuingAirline> 
        <TicketOrFIMCouponNumber>1</TicketOrFIMCouponNumber> 
        <TicketDocOrFIMNumber>2649488544</TicketDocOrFIMNumber> 
        <CheckDigit>6</CheckDigit> 
        <CurrAdjustmentIndicator>USD</CurrAdjustmentIndicator> 
        <ElectronicTicketIndicator>E</ElectronicTicketIndicator> 
        <AirlineFlightDesignator>MR</AirlineFlightDesignator> 
        <FlightNo>885</FlightNo> 
        <FlightDate>2013-04-03</FlightDate> 
        <FromAirportCode>ULN</FromAirportCode> 
        <ToAirportCode>HKG</ToAirportCode> 
        <SettlementAuthorizationCode>861FBKOPVEZZ4</SettlementAuthorizationCode> 
        <Attachment>
            <AttachmentIndicatorOriginal>N</AttachmentIndicatorOriginal> 
        </Attachment>
    </CouponDetails>
</LineItemDetail>

以上是 XML 文档。我需要检索标签的值<TaxCode>

直到现在我已经走到了这一步

var tax = from d in doc.Root.Descendants("Tax") select d;

foreach (var p in tax)
{
    taxcode= p.Element("TaxCOde").Value;
}
4

4 回答 4

1

p.Element("TaxCOde")p仅检查( element)的直接子级<Tax>,因此它什么也找不到。

试试看:

foreach (var p in tax)
{
    taxcode= (string)p.Element("TaxBreakdown").Element("TaxCode");
}

我使用(string)XElement转换而不是属性,因为找不到元素时XElement.Value它不会抛出。NullReferenceException

编辑

示例 XML 的测试代码:

var doc = XDocument.Load("source.txt");

var lineItemDetails = doc.Descendants("LineItemDetail");

foreach (var lineItemDetail in lineItemDetails)
{
    var tax = lineItemDetail.Element("Tax");
    var taxCode = (string)tax.Element("TaxBreakdown").Element("TaxCode");
}

taxCodeMN

于 2013-08-12T06:07:58.197 回答
0

试试这个

 foreach (var p in tax)
 {
   taxcode= p.Element("TaxBreakdown").Element("TaxCode").Value;
 }
于 2013-08-12T06:12:38.383 回答
0
  XDocument DocumentObject = XDocument.Load(yourxml); 
  IEnumerable<XElement> Tax= from TaxInfo in DocumentObject.Descendants("Tax") select  TaxInfo;

            foreach (var t in Tax)
            {
                TaxType = (string)t.Element("TaxType");

            }
于 2013-08-12T06:56:32.503 回答
-1

为什么不尝试使用 XmlDocument?您已经在文档中加载了 xml,您可以这样做(我已将 XmlDocument 声明为doc):

foreach(XmlElement elem in doc.GetElementsByTagName("TaxCOde"))
     taxcode = elem.InnerText;

我只是用我的记忆写的,所以如果我犯了一些语法或语法错误,我很抱歉

于 2013-08-12T06:24:50.067 回答