简体   繁体   中英

How to parse nested elements using LINQ to XML

I have an XML file with multiple checkItem elements. I need to save each checkItem element into a database. I'm having a difficult time getting exactly what I need using the query below.

<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>

Here is the query I've been working with. I've tried several different ways with no luck. Without the use of .Concat, I cannot get the other elements; however, using .Concat does not allow me to get all values in a manageable format. I need to separate the Front and Rear imageViews based on the ViewSide value, and only need the imageRefKey and imageFileLocator values from the imageView element. Can anyone point me in the right direction?

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")
                   });

You can easily get nested elements of any XElement by just calling the Elements() method of that instance, then calling Select() on that collection, to created a nested collection of an anonymous type in your main anonymous type.

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
                       }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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