I have this xml file:
<?xml version="1.0" encoding="utf-8"?>
<FieldsDefinitions>
<FieldsDefinition>
<Field>DEMOFIELD</Field>
<Length>15</Length>
<Label>IIS: </Label>
<Type>DropDown</Type>
</FieldsDefinition>
<FieldsDefinition>
<Field>IIS</Field>
<Length>15</Length>
<Label>IIS: </Label>
<Type>DropDown</Type>
</FieldsDefinition>
<FieldsDefinition>
<Field>DEMOFIELD</Field>
<Length>20</Length>
<Label>Demo Field</Label>
<Type>Text</Type>
</FieldsDefinition>
</FieldsDefinitions>
If I want to get all fields with value=DEMOFIELD, I could do something like this:
XDocument xDoc = XDocument.Load("file.xml");
var x = xDoc.Descendants("Field").Where(elem => elem.Value == "DEMOFIELD");
foreach(XElement e in x)
{
_log.Debug(e.Name + " = " + e.Value);
}
But how can I query for field='DEMOFIELD' and label='IIS:'? Do I use a sort of multi-where?
You probably want to step one level up and find all FieldsDefinition
elements that match your criteria:
var fieldDefs = doc
.Descendants("FieldsDefinition")
.Where(x => (string) x.Element("Field") == "DEMOFIELD" &&
(string) x.Element("Label") == "IIS: ");
See this fiddle for a working demo.
You should query for FieldsDefinition
elements:
var x = from fd in xDoc.Descendants("FieldsDefinition")
where (string)fd.Element("Field") == "DEMOFIELD"
&& (string)fd.Element("Label") == "IIS: "
select fd;
NOTE: I would recomend to trim value of label to avoid different number of spaces affect results:
&& ((string)fd.Element("Label")).Trim() == "IIS:"
Also if you are absolutely sure that every FieldsDefinition
has both Field
and Label
element, then you can use Value
field directly:
var x = xDoc.Descendants("FieldsDefinition")
.Where(fd => fd.Element("Field").Value == "DEMOFIELD"
&& fd.Element("Label").Value.Trim() == "IIS:");
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.