如何从动态代码中使用的特定节点访问子节点?

时间:2019-05-05 10:29:53

标签: c# xml

所以我想从特定节点获取NodeList。在此课程中,我正在编写一个程序来复制银行作为“最终”形式,并且我编写了一个XML文档,其中包含我银行的所有客户。我已经为登录功能编写了代码,现在我想返回特定客户端的详细信息。

<clients>
    <client>
        <name>Victor Olin</name>
        <ssid>20000724</ssid>
        <pswd>storabanken</pswd>
        <savingsAcc>
          <name>Victors Sparkonto</name>
          <accountNumber>111-222-333</accountNumber>
          <balance>5000</balance>
        </savingsAcc>
        <regularAcc>
          <name>Victors Lönekonto</name>
          <accountNumber>222-333-444</accountNumber>
          <balance>314.15</balance>
        </regularAcc>
    </client>
</clients>

所以想要的结果是,我想要一个包含该特定客户端具有的所有“ savingsAcc”和“ regularAcc”的NodeList,以便我可以访问所有内部文本并将其发送给客户端。当客户端使用正确的凭据登录时,将指定正确的客户端,并且我认为该节点可以充当根元素,但是我不确定如何实现它。

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
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement client in doc.Descendants("client"))
            {
                Client newClient = new Client();
                Client.clients.Add(newClient);

                newClient.name = (string)client.Element("name");
                newClient.ssid = (string)client.Element("ssid");
                newClient.pswd = (string)client.Element("pswd");

                List<XElement> xAccounts = client.Elements().Where(x => (x.Name.LocalName == "savingsAcc") || (x.Name.LocalName == "regularAcc")).ToList();
                foreach (XElement xAccount in xAccounts)
                {
                    Account newAccount = null;
                    switch (xAccount.Name.LocalName)
                    {
                        case "savingsAcc" :
                            newAccount = new SavingsAccount();
                            break;
                        case "regularAcc":
                            newAccount = new RegularAccount();
                            break;
                    }
                    newClient.accounts.Add(newAccount);

                    newAccount.name = (string)xAccount.Element("name");
                    newAccount.accountNumber = (string)xAccount.Element("accountNumber");
                    newAccount.balance = (decimal)xAccount.Element("balance");
                }
            }
        }
    }
    public class Client
    {
        public static List<Client> clients = new List<Client>();
        public string name { get; set; }
        public string ssid { get; set; }
        public string pswd { get; set; }
        public List<Account> accounts = new List<Account>();
    }
    public class Account
    {
        public string name { get; set; }
        public string accountNumber { get; set; }
        public decimal balance { get; set; }
    }
    public class SavingsAccount : Account 
    {
    }
    public class RegularAccount : Account
    {
    }
}