简体   繁体   中英

How to read specific field value from xml using linq in C#

Here is the xml iam trying to read.

In the below given xml i want to read "customfieldvalue" where customfieldname = "Fixed in Build".

        - <customfield id="customfield_10021" key="x">
          <customfieldname>Date of First Response</customfieldname> 
        - <customfieldvalues>
          <customfieldvalue>Thu, 27 Mar 2014 00:28:36 -0700</customfieldvalue> 
          </customfieldvalues>
          </customfield>
        - <customfield id="customfield_10034" key="x">
          <customfieldname>Fixed in Build</customfieldname> 
        - <customfieldvalues>
        - <customfieldvalue>
          <a href="url" title="[M8960AAAAANLGD2322586.1] - Apps Crash - Internal error: (FSR = 0x5) (PC = msm_rpmrs_lowest_limits+0x8c/0x240)">144148</a> 
          </customfieldvalue>
          </customfieldvalues>
          </customfield>

And My code is :

 var result1 = from feed in XDocument.Parse(_result.ToString()).Descendants("customfields")
                  .Where(x => x.Element("customfieldname").Value == "Fixed in Build")
                  .Elements("customfieldvalues").First()
                  .Select(
                      x => x.Element("customfieldvalue").Value

                  );

I have also tried it in different ways to get the result but ,no use

some boby help me in this.

customfields is not in the sample but I guess it's the node surrounding this. You probably need to lose an 's' :

//.Descendants("customfields")
  .Descendants("customfield")
  .Where(...)

or, when you want to be more precise about the XML structure:

 .Descendants("customfields")
 .Elements("customfield")
 .Where(...)

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