如何动态获取具有相同名称id的xml文件的最后一个元素

时间:2015-11-03 17:15:52

标签: xml elements

包含系列列表的Xml文件。一个家庭可以随时添加到列表中,也可以添加一个新的孩子..

<family>
 <Mother>
      <child age="1">
       <dog-name>dog1</dog-name>
      </child>
      <child age="2">
       <dog-name>dog1</dog-name>
      </child>
      <child age="3">
       <dog-name>dog1</dog-name>
      </child>
      <child age="4">
        <dog-name>dog1</dog-name>
      </child>
   </Mother>
</family>
<family2>
   <Mother>
      <child age="1">
       <dog-name>dog1</dog-name>
      </child>
      <child age="2">
       <dog-name>dog1</dog-name>
      </child>
      <child age="3">
       <dog-name>dog1</dog-name>
      </child>
      <child age="4">
        <dog-name>dog1</dog-name>
      </child>
   </Mother>
</family2>
<family3>
   <Mother>
      <child age="1">
       <dog-name>dog1</dog-name>
      </child>
      <child age="2">
       <dog-name>dog1</dog-name>
      </child>
      <child age="3">
       <dog-name>dog1</dog-name>
      </child>
      <child age="4">
        <dog-name>dog1</dog-name>
      </child>
   </Mother>
</family3>

我正在尝试阅读任何一个家庭中年龄最大的孩子的年龄,但最多可以将新孩子添加到xml中。

我试过了:

while (xOtherReader.Read())
        {
            if (xOtherReader.NodeType == XmlNodeType.Element &&    xOtherReader.Name == "family2")
            {
                xOtherReader.ReadToFollowing("Mother");

                if (xOtherReader.NodeType == XmlNodeType.Element && xOtherReader.Name == "Mother")
                {
                    xOtherReader.ReadToFollowing("child");
                    if (xOtherReader.NodeType == XmlNodeType.Element && xOtherReader.Name == "child")
                    {
                        xOtherReader.ReadEndElement();
                        Console.Write(xOtherReader.GetAttribute("age");
                    }
                }
            }
        }

我还希望得到最年长的孩子的狗的名字,然后把价值放在......

1 个答案:

答案 0 :(得分:0)

使用XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Root>" +
                "<family>" +
                     "<Mother>" +
                          "<child age=\"1\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"2\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"3\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"4\">" +
                            "<dog-name>dog1</dog-name>" +
                          "</child>" +
                       "</Mother>" +
                    "</family>" +
                    "<family2>" +
                       "<Mother>" +
                          "<child age=\"1\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"2\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"3\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"4\">" +
                            "<dog-name>dog1</dog-name>" +
                          "</child>" +
                       "</Mother>" +
                    "</family2>" +
                    "<family3>" +
                       "<Mother>" +
                          "<child age=\"1\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"2\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"3\">" +
                           "<dog-name>dog1</dog-name>" +
                          "</child>" +
                          "<child age=\"4\">" +
                            "<dog-name>dog1</dog-name>" +
                          "</child>" +
                       "</Mother>" +
                    "</family3>" +
                 "</Root>"; ;

            XElement family = XElement.Parse(xml);

            var results = family.Elements().Select(x => new
            {
                name = x.Name,
                children = x.Descendants("child").Select(y => new
                {
                    age = (int)y.Attribute("age"),
                    dog_name = y.Element("dog-name").Value
                }).OrderByDescending(z => z.age).ToList()
            }).ToList();

            foreach (var family_properties in results)
            {
                Console.WriteLine("Family Name : {0}, Age = {1}, Dog Name = {2}",
                    family_properties.name, family_properties.children.FirstOrDefault().age, family_properties.children.FirstOrDefault().dog_name); 
            }
            Console.ReadLine();
        }
    }
}
​