如何使用LINQ to XML解析嵌套元素

时间:2014-08-19 20:46:39

标签: c# xml linq

我有一个包含多个checkItem元素的XML文件。我需要将每个checkItem元素保存到数据库中。我很难用下面的查询获得我需要的东西。

<checkItem>
    <checkItemType>check</checkItemType>
    <checkAmount>195000</checkAmount>
    <nonMICRCheckData>
      <legalAmount>195000</legalAmount>
      <issueDate>2010-04-30</issueDate>
      <other>PAY VAL 20 CHARACTER</other>
    </nonMICRCheckData>
    <postingInfo>
      <date>2013-05-01</date>
      <RT>10108929</RT>
      <accountNumber>111111111</accountNumber>
      <seqNum>11111111</seqNum>
      <trancode>111111</trancode>
      <amount>195000</amount>
      <serialNumber>1111111</serialNumber>
    </postingInfo>
    <totalImageViewsDelivered>2</totalImageViewsDelivered>
    <imageView>
      <imageIndicator>Actual Item Image Present</imageIndicator>
      <imageViewInfo>
        <Format>
          <Baseline>TIF</Baseline>
        </Format>
        <Compression>
          <Baseline>CCITT</Baseline>
        </Compression>
        <ViewSide>Front</ViewSide>
        <imageViewLocator>
          <imageRefKey>201305010090085000316000085703_Front.TIF</imageRefKey>
          <imageFileLocator>IFTDISB20130625132900M041.zip</imageFileLocator>
        </imageViewLocator>
      </imageViewInfo>
      <imageViewInfo>
        <Format>
          <Baseline>TIF</Baseline>
        </Format>
        <Compression>
          <Baseline>CCITT</Baseline>
        </Compression>
        <ViewSide>Rear</ViewSide>
        <imageViewLocator>
          <imageRefKey>201305010090085000316000085703_Rear.TIF</imageRefKey>
          <imageFileLocator>IFTDISB20130625132900M041.zip</imageFileLocator>
        </imageViewLocator>
      </imageViewInfo>
    </imageView>
  </checkItem>

以下是我一直在使用的查询。我没试过几种不同的方法。不使用.Concat,我无法获得其他元素;但是,使用.Concat不允许我以可管理的格式获取所有值。我需要根据ViewSide值分离Front和Rear imageViews,并且只需要imageView元素中的imageRefKey和imageFileLocator值。谁能指出我正确的方向?

var query = doc.Descendants("checkItem")
            //.Concat(doc.Descendants("postingInfo"))
            //.Concat(doc.Descendants("imageViewLocator"))//.Where(x => (string)x.Element("ViewSide") == "Front"))
            //.Concat(doc.Descendants("imageViewInfo").Where(x => (string)x.Element("ViewSide") == "Rear"))
            .Select(x => new {
                       CheckAmount = (string) x.Element("checkAmount"),
                       ImageRefKey = (string) x.Element("imageRefKey"),
                       PostingDate = (string) x.Element("dare"),
                       //FrontViewSide = (string) x.Element("ViewSide"),
                       //RearViewSide = (string) x.Element("BViewSide")
                   });

1 个答案:

答案 0 :(得分:0)

通过调用该实例的XElement方法,然后在该集合上调用Elements(),创建嵌套的匿名类型集合,您可以轻松获取任何Select()的嵌套元素在你的主要匿名类型。

var query = doc.Elements("checkItem")
               .Select( x =>
               new 
               {
                   CheckAmount = (string) x.Element("checkAmount"),
                   ImageRefKey = (string) x.Element("imageRefKey"),
                   PostingDate = (string) x.Element("dare"),
                   ImageViews = x.Element("ImageView").Elements("ImageViewInfo")
                       .Select(iv=> 
                       new 
                       {
                          Format = iv.Element("Format").Element("Baseline").Value
                          // more parsing here
                       }
                }