2015-11-03 44 views
0

Xml包含族的列表的文件。一個家庭可以被添加到列表在任何時間,還可以添加新的子..如何動態獲取具有相同名稱的xml文件的最後一個元素ID

<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"); 
        } 
       } 
      } 
     } 

我也想獲得最大的孩子的狗的名字和出把價值..

回答

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(); 
     } 
    } 
} 
​ 
+0

是作品完美的歡迎@jdweng我應該在開始時指定xml來自鏈接/ url,它是否也適用於在線url? – Decoder94

+0

我應該用XElement family = XElement.Load(url); url是一個字符串(文件或url)。 – jdweng

相關問題