gpt4 book ai didi

c# - 从 xml 文件加载控件

转载 作者:行者123 更新时间:2023-11-30 22:47:28 24 4
gpt4 key购买 nike

我知道如何写入 xml 文件,但我在做我需要做的事情时遇到了麻烦,而且找不到关于此类问题的足够信息。

下面是一个 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<controls>
<Label Content="Double-click to edit." Location="258, 178" Size="101, 13" ForeColor="-1" />
<LinkLabel Content="Double-click to edit." Location="532, 133" Size="101, 13" LinkColor="-1" />
<LinkLabel Content="Double-click to edit." Location="424, 212" Size="101, 13" LinkColor="-1" />
<Label Content="Double-click to edit." Location="282, 89" Size="101, 13" ForeColor="-1" />
<Label Content="Double-click to edit." Location="528, 178" Size="101, 13" ForeColor="-1" />
<LinkLabel Content="Double-click to edit." Location="528, 133" Size="101, 13" LinkColor="-1" />
<LinkLabel Content="Double-click to edit." Location="528, 149" Size="101, 13" LinkColor="-1" />
<Label Content="Double-click to edit." Location="528, 164" Size="101, 13" ForeColor="-1" />
</controls>

在我的应用程序中打开此文件后,我需要做的是:

foreach(control in XmlFile)
{

get Content
get Location
get Size
get ForeColor/LinkColor
// do something...
}

有人可以帮我解决这个问题吗?如果有任何帮助,我将不胜感激。

谢谢

贝尔

最佳答案

使用 LINQ:(对于 ForeColor/LinkColor 检查是否为 null)

XDocument loaded = XDocument.Load(@"C:\XMLFile1.xml");

var q = from c in loaded.Descendants().Descendants()
select new
{
content = c.Attribute("Content"),
location = c.Attribute("Location"),
size = c.Attribute("Size"),
foreColor = c.Attribute("ForeColor"),
linkColor = c.Attribute("LinkColor")
};

foreach (var controlItem in q)
Console.WriteLine("Control content = {0}", controlItem.content);

关于c# - 从 xml 文件加载控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2217401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com