如何获取XDocument子节点

时间:2016-02-24 10:19:17

标签: c# xml



<Users>
  <Account>
    <User>
      <Username>AdminUser</Username>
      <Password>pass123</Password>
      <Active>true</Active>
      <Account>Admin</Account>
      <Details>
  	<Account>xyz</Account>
      </Details>
    </User>   
  </Account>
 <Account>
    <User>
      <Username>AdminUser</Username>
      <Password>pass123</Password>
      <Active>true</Active>
      <Account>Admin</Account>
      <Details>
  	<Account>xyz</Account>
      </Details>
    </User>   
  </Account>
 <Account>
    <User>
      <Username>AdminUser</Username>
      <Password>pass123</Password>
      <Active>true</Active>
      <Account>Admin</Account>
      <Details>
  	<Account>xyz</Account>
      </Details>
    </User>   
  </Account>
</Users>
&#13;
&#13;
&#13;

反序列化后,我得到如上所示的XDocument。 &#34;帐户&#34;节点具有相同名称的子节点,即Account。如何获取&#34;用户&#34;的所有子节点。节点意味着我想得到三个&#34;帐户&#34;节点&#34;用户&#34;节点

3 个答案:

答案 0 :(得分:1)

IEnumerable<XElement> accounts = doc.Root.Elements("Account");

在此示例中Account是唯一的直接子女,您可以使用this overload

IEnumerable<XElement> accounts = doc.Root.Elements();

您应该使用XContainer.Elements("name")来获取直接子项,或者XContainer.Descendants("name")如果您想获得具有相同名称的所有嵌套元素。

答案 1 :(得分:1)

如果我理解正确,请尝试使用Xpath Query(经过测试并通过)

    var xml_string = "<Users>  <Account>    <User>      <Username>AdminUser</Username>      <Password>pass123</Password>      <Active>true</Active>      <Account>Admin</Account>      <Details>    <Account>xyz</Account>      </Details>    </User>     </Account> <Account>    <User>      <Username>AdminUser</Username>      <Password>pass123</Password>      <Active>true</Active>      <Account>Admin</Account>      <Details>      <Account>xyz</Account>      </Details>    </User>     </Account> <Account>    <User>      <Username>AdminUser</Username>      <Password>pass123</Password>      <Active>true</Active>      <Account>Admin</Account>      <Details>      <Account>xyz</Account>      </Details>    </User>     </Account></Users>";

    var xmlNodes = xml.SelectNodes("Users/Account/User");

    foreach (XmlNode node in xmlNodes)
    {
      Console.WriteLine("Username: {0}; Password: {1}; Active:{2}; Account: {3}; Details Account: {4}",
                                node["Username"].InnerText,
                                node["Password"].InnerText,
                                node["Active"].InnerText,
                                node["Account"].InnerText,
                                node["Details"].InnerText);
    }

  //result is

   // Username: AdminUser; Password: pass123; Active:true; Account: Admin; Details Account: xyz
   // Username: AdminUser; Password: pass123; Active:true; Account: Admin; Details Account: xyz
  // Username: AdminUser; Password: pass123; Active:true; Account: Admin; Details Account: xyz"

User node

下获取3个Account个节点

或者:获取Account

下的3 Users/Account个节点
            var xml = new XmlDocument();

            xml.LoadXml(xml_string);

            var xmlNodes = xml.SelectNodes("Users/Account/User/Account");

            foreach (XmlNode node in xmlNodes)
            {
               Console.WriteLine(node.InnerText);
            }

           //result is 

            //Admin
            //Admin
            //Admin

答案 2 :(得分:1)

 XDocument doc = XDocument.Load(path);
        foreach (XElement element in doc.Root
                              .Element("Account")
                              .Elements("User"))
        {
            Console.WriteLine("Username: {0}; Password: {1}; Active:{2}; Account: {3}; Details Account: {4}",
                              (string)element.Element("Username"),
                              (string)element.Element("Password"),
                              (string)element.Element("Active"),
                              (string)element.Element("Account"),
                              (string)element.Element("Details").Element("Account"));
        }