通过XElement从XML文件读取C#读取值依赖内容

时间:2014-04-28 11:56:14

标签: c# xml xelement

我必须在C#中读取所有与用户名相关的设置(参数:'名称'以及'可见'),然后按顺序将名称存储到列表' name& #39;然后可见'等等。

所以列表内容应该是(在客户端 - 用户名:'服务'):

Name-of-Service-Setting-1
True1S
Name-of-Service-Setting-2
True2S
Name-of-Service-Setting-3
True3S

但我只收到第一个孩子(客户名称:'服务'):

Name-of-Service-Setting-1
True1S

这是我的C#代码:

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

namespace LinqToXML_Example
{
    public class Program
    {
        public static bool IsUsernameExisting(string username, XElement clients)
        {
            var userName = from p in clients.Elements()
                           where p.Element("Username").Value == username
                           select p.Element("Username").Value;

            foreach (var p in userName)
            {
                return true;
            }

            return false;
        }



        public static List<string> ReadUserSettings(string username, XElement clients)
        {

            List<string> settingsList = new List<string>();


            if (IsUsernameExisting(username, clients))
            {
                var setting = from s in clients.Elements()
                              where s.Element("Username").Value == username
                              select s.Element("Settings");

                foreach (var p in setting)
                {
                    settingsList.Add(p.Element("Setting").Element("Name").Value);
                    settingsList.Add(p.Element("Setting").Element("Visible").Value);
                }

                return settingsList;
            }


            var errorMsg = "Cannot get the username's settings, because of a wrong Username!";
            settingsList.Add(errorMsg);

            return settingsList;
        }





        public static void Query(string username, XElement clients)
        {
            // Readout the Settings of Client:
            Console.WriteLine("Readout all the settings of client " + "'" + username + "':");

            List<string> resultList1 = new List<string>();
            resultList1 = ReadUserSettings(username, clients);

            if (resultList1.Count != 1)
            {
                for (int i = 0; i < resultList1.Count(); i++)
                {
                    Console.WriteLine(resultList1.ElementAt(i));
                }
            }

            else Console.WriteLine(resultList1.ElementAt(0));


            Console.WriteLine("\n- - - - - End-Of-File- - - - - ");
            Console.Read();
        }




        static void Main()
        {

            var asm = Assembly.GetExecutingAssembly();
            var textStream = asm.GetManifestResourceStream("LinqToXML_Example.AccessData.xml");
            var xmlReader = new XmlTextReader(textStream);

            XElement clients = XElement.Load(xmlReader);


            Query("Service", clients);
        }
    }
}

这是我的AccessData.xml文件:

  

<Client>

  <Username>Administrator</Username>

  <Password>Admin-Password</Password>

  <Settings>

    <Setting>
      <Name>Name-of-Admin-Setting-1</Name>
      <Visible>True1A</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Admin-Setting-2</Name>
      <Visible>True2A</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Admin-Setting-3</Name>
      <Visible>True3A</Visible>
    </Setting>

  </Settings>

</Client>



<Client>

  <Username>Service</Username>

  <Password>Service-Password</Password>

  <Settings>

    <Setting>
      <Name>Name-of-Service-Setting-1</Name>
      <Visible>True1S</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Service-Setting-2</Name>
      <Visible>True2S</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Service-Setting-3</Name>
      <Visible>True3S</Visible>
    </Setting>

  </Settings>

</Client>



<Client>

  <Username>Customer</Username>

  <Password>Customer-Password</Password>

  <Settings>

    <Setting>
      <Name>Name-of-Customer-Setting-1</Name>
      <Visible>True1C</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Customer-Setting-2</Name>
      <Visible>True2C</Visible>
    </Setting>

    <Setting>
      <Name>Name-of-Customer-Setting-3</Name>
      <Visible>True3C</Visible>
    </Setting>

  </Settings>

</Client>

1 个答案:

答案 0 :(得分:0)

更改此foreach循环:

foreach (var p in setting)
{
     settingsList.Add(p.Element("Setting").Element("Name").Value);
     settingsList.Add(p.Element("Setting").Element("Visible").Value);
}

要:

foreach (var p in setting.Elements("Setting"))
{
     settingsList.Add(p.Element("Name").Value);
     settingsList.Add(p.Element("Visible").Value);
}

每个Settings下只有一个Client元素。因此,您的循环运行一次,而您使用p.Element("Setting")获取第一个项目,而不是迭代子元素Settings。而且,您可以考虑使用List<string>代替Dictionary