检索子节点值

时间:2013-08-12 06:04:40

标签: c# xml linq-to-xml

这是我正在使用的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 个答案:

答案 0 :(得分:1)

p.Element("TaxCOde")仅检查p<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

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分: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");

            }

答案 3 :(得分:-1)

为什么不尝试使用XmlDocument?你已经在文档中加载了xml(我已经将XmlDocument声明为doc):

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

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