简体   繁体   English

使用linq c#在单个节点上返回多个具有相同名称的xml子元素#

[英]Returning multiple xml Children with same name on single node using linq c#

I have the following XML and want to return all "schools" children but I only get the first one. 我有以下XML,并希望返回所有“学校”的孩子,但我只有第一个。 (jeffersion/08.36) I looked high and low and banged my head. (jeffersion / 08.36)我高高低低地抬起头来。 What am I missing? 我错过了什么?

<users>
  <user>
    <role>janitor</role>
    <schools>
      <school_name>jefferson</school_name>
      <keycode>80.36</keycode>
      <school_name>mainline</school_name>
      <keycode>64.36</keycode>
      <school_name>south side</school_name>
      <keycode>31</keycode>
    </schools>
  </user>
</users>

This is only returning the first record. 这只返回第一条记录。

var results= from schools in myXmlDoc.Descendants("schools")
                   select new 
                   {
                       SchoolName = schools.Element("school_name").Value,
                       KeyCode = schools.Element("keycode").Value
                   };

I've also tried: 我也尝试过:

var results= (from schools in myXmlDoc.Descendants("schools")
                   select new 
                   {
                       SchoolName = schools.Element("school_name").Value,
                       KeyCode = schools.Element("keycode").Value
                   }.ToList();

This gets the values BUT only for the first school: 这只获得了第一所学校的价值观:

var schools = (from c in xml.Descendants("user")
                      select new
                      {
                          Name = c.Element("role").Value,
                          Fields = c.Elements("schools")
                              .Select(f => new
                              {
                                  SchoolName = f.Element("school_name").Value,
                                  Keycode = f.Element("keycode").Value
                              }).ToArray()
                      }).ToList();

This may be helpful: 这可能会有所帮助:

var result = from c in XElement.Load("Student.xml").Elements("schools") select c ; var result =来自XElement.Load中的c(“Student.xml”)。元素(“学校”)选择c;

// Execute the query foreach (var students in result ) { //do something } //对foreach执行查询(结果是var个学生){//做某事}

You only have one <schools> element in your source, which is why only one entry is being returned. 源中只有一个<schools>元素,这就是为什么仅返回一个条目的原因。 The XML isn't particularly nicely structured - it would be good to have a <school> element containing each school_name/keycode pair. XML的结构不是特别好 - 最好有一个包含每个school_name / keycode对的<school>元素。 But assuming you have to live with it, the following should work: 但是,假设您必须忍受它,那么以下方法应该起作用:

var results= from school in myXmlDoc.Descendants("school_name")
               select new 
               {
                   SchoolName = school.Value,
                   KeyCode = school.ElementsAfterSelf("keycode").First().Value
               };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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