[英]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. 我必须在C#中读出所有与用户名相关的设置(参数:“名称”和“可见”),并按“名称”然后“可见”的顺序将其存储到列表中。
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'): 但是我只有第一个孩子(在客户端用户名:“ Service”处):
Name-of-Service-Setting-1
True1S
Here is my C#-code: 这是我的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);
}
}
}
This is my AccessData.xml-file: 这是我的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>
Change this foreach loop: 更改此foreach循环:
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
. 只有一个Settings
元素下的每个Client
。所以一周时间运行的循环,你正在使用得到的第一个项目p.Element("Setting")
而不是你应该遍历的子元素Settings
。还有,而不是List<string>
您可能要考虑使用Dictionary
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.