简体   繁体   中英

how to query xml using linq or xpath

In the xml below, I would like to query the xml by matricule and then loop through the HO elements and insert them in a .net list.

I tried the following code but it does not work:

private List<Habilitation> GetAgents(string matricule)
 {
 List<Habilitation> haList = new List<Habilitation>();   

 IEnumerable<XElement> list = xdoc.XPathSelectElements("//agents/agent/data[matricule=matricule"]);
                foreach (XElement ho in list)
                {
                    Habilitation ha = new Habilitation();
                    ha.Matricule = (string)ho.Element("matricule");
                    ha.H_Domain = (string)ho.Parent.Element("questions").Element("H0").Element("H_Domain");
                    ha.H_Environment = (string)ho.Parent.Element("questions").Element("H0").Element("H_Environment");
                    ha.H_Mastered = (string)ho.Parent.Element("questions").Element("H0").Element("H_Mastered");
                    haList.Add(ha);
                }


                return haList;
                }

Xml

<agents xmlns="">
    <agent>
      <data>
        <name>Agent1</name>
        <matricule>123456</matricule>
      </data>
      <questions>
        <H0>
          <H_Domain>RAC Raccordement Basse Tension12</H_Domain>
          <H_Environment>CPT travaux comptage12</H_Environment>
          <H_Mastered>0</H_Mastered>
        </H0>
        <H0>
          <H_Domain>IBT Intervention r�seau Basse Tension</H_Domain>
          <H_Environment>TIA Travaux aux installations a�riennes</H_Environment>
          <H_Mastered>1</H_Mastered>
        </H0>
        <H0>
          <H_Domain>IBT Intervention r�seau Basse Tension</H_Domain>
          <H_Environment>AEM Actes d'exploitation et man�uvre</H_Environment>
          <H_Mastered>1</H_Mastered>
        </H0>
      </questions>
    </agent>
    <agent>
      <data>
        <name>Agent2</name>
        <matricule>1234567</matricule>
      </data>
      <questions>
        <H0>
          <H_Domain>HTS Haute Tension Souterraine11111111555</H_Domain>
          <H_Environment>AEM Actes d'exploitation et man�uvre</H_Environment>
          <H_Mastered>1</H_Mastered>
        </H0>
      </questions>
    </agent>
    <agent>
      <data>
        <name>Agent3</name>
        <matricule>1234568</matricule>
      </data>
      <questions>
        <H0>
          <H_Domain>IBT Intervention r�seau Basse Tension</H_Domain>
          <H_Environment>TIA Travaux aux installations a�riennes</H_Environment>
          <H_Mastered>0</H_Mastered>
        </H0>
      </questions>
    </agent>
  </agents>

试试这个代码:

var node = doc.Descendants("agent").Elements("questions").Elements("H0");

You can try this way :

.....
var xpath = String.Format("//agents/agent[data/matricule='{0}']/questions/H0", matricule);
IEnumerable<XElement> list = xdoc.XPathSelectElements(xpath);
foreach (XElement ho in list)
{
    Habilitation ha = new Habilitation();
    ha.Matricule = matricule;
    ha.H_Domain = (string)ho.Element("H_Domain");
    ha.H_Environment = (string)ho.Element("H_Environment");
    ha.H_Mastered = (string)ho.Element("H_Mastered");
    haList.Add(ha);
}
.....

Or using LINQ to create List<Habilitation> replacing your foreach :

var haList = (from ho in xdoc.XPathSelectElements(xpath);
              select select new Habilitation
                             {
                                 Matricule = matricule,
                                 H_Domain = (string)ho.Element("H_Domain"),
                                 H_Environment = (string)ho.Element("H_Environment"),
                                 H_Mastered = (string)ho.Element("H_Mastered")
                             }).ToList();

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