简体   繁体   中英

C# readout value-dependent content from XML-file via XElement

I have to readout all the username-dependent settings (parameter: 'name' and 'visible') in C# and store it to an List in the order 'name' then 'visible' and so on.

So the list-content should be (at client-username: 'Service'):

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

But I only get the first child (at client-username: 'Service'):

Name-of-Service-Setting-1
True1S

Here is my C#-code:

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

This is my AccessData.xml-file:

<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>

Change this foreach loop:

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

To:

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

There is only one Settings element under each Client .So your loop running for one time and you are getting the first item using p.Element("Setting") , instead you should iterate over the child elements of Settings .Also, instead of List<string> you may want to consider using a Dictionary .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM