Sunday 15 September 2013

c# - Sequence contains no elements when querying XML file by attribute ID -


I have a slightly confusing problem with my Linux query against an XML file. This attribute value is correctly (< Code> GB ). However, when I give a different attribute value, for example: jE I have no element in a sequence error, still the element is explicitly in the XML document is present?

This is my query:

  Return (n to xml.Descendants ("postalCodeData") where n.Element ("postCodeRegex"). Attribute ("regionId") Select value == iSCode n.Element ("postCodeRegex").). Single (). ToString ();   

Here are some sample XML data:

  & lt; PostalCodeData & gt; & Lt; PostCodeRegex regionId = "GB" & gt; GIR []? 0AA | ((BB | BL | BA | Bb | BD | BH | BL | BN | BR | BS | BT | CA | CB | CF | CH | CM | CO | CR | CT | CV | CW | DA | DD | DAY | DG | DH | DL | DN | DT | DY | E | EC | EH | N | Former | FK | Financial Year | G | GL | GY | GU | H | HD | HG | Himachal Pradesh | HR | HS | HU | HX | IG | IM | IP | IV | JE | KA | KT | KW | KY | L | LA | LY | LE | LLLL | LN | Lok Sabha | LU | M | ME | MK | A L | n | ne | ng | nn | np | nr | nw | official language | ox | PA | pe | pH | pl | p | pr | rg | r | rm | s | sa | se | sg | shk | SL | SM | SN | As | SP | SR | SS | Scheduled Tribe | SW | SY | Territorial Army | TD | TF | TamilNadu | TQ | TR | TS | TW | UB | W | Washington | WC | WD | WF | WN | WR | WS | WV | YO | ZE) (\ d [\ dA-Z]? []? \ D [ABD-HJLN-UW-Z] {2})). BFPO []? \ D {1,4} & lt; / PostCodeRegex & gt; & Lt; PostCodeRegex regionId = "JE" & gt; Je \ d [\ dA-Z]? []? \ D [ABD-HJLN-UW-Z] {2} & lt; / PostCodeRegex & gt; & Lt; PostCodeRegex regionId = "GG" & gt; JY \ d [\ dA-Z]? []? \ D [ABD-HJLN-UW-Z] {2} & lt; / PostCodeRegex & gt; & Lt; / PostalCodeData & gt;   

Is there any place that I have done wrong here? Why will it work for GB and for others?

  N.Element ("postCodeRegex")   

The first element of that type will return, and its "regionId" will be examined.

Instead, with:

  where n.Elements ("postCodeRegex"). Edit any: (element = & gt; element.Attribute ("regionId"). Value == isoCode)   

Edit: OK, so you wanted matching copied codegax item. I thought that you wanted to bundle the first element with the match. Then the solution is very simple.

  Return (n to xml.Descendants ("postCodeRegex") where n.Attribute ("regionId"). Value == isoCode n.ToString () Select;;  < / Pre>  

No comments:

Post a Comment