C#通过XElement从XML文件中读取与值相关的内容
本文关键字:读取 XElement 通过 XML 文件 | 更新日期: 2023-09-27 18:00:27
我必须读出C#中所有与用户名相关的设置(参数:"name"answers"visible"),并将其按"name"然后"visible'"的顺序存储到列表中,依此类推
因此列表内容应为(客户端用户名:"Service"):
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>
更改此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);
}
每个Client
下只有一个Settings
元素。因此,循环运行一次,并且您使用p.Element("Setting")
获得第一个项目,因此您应该迭代Settings
的子元素。此外,您可能需要考虑使用Dictionary
而不是List<string>
。