【问题标题】:Retrieving multiple attributes from XML file using LINQ to XML, writing each to line使用 LINQ to XML 从 XML 文件中检索多个属性,将每个属性写入行
【发布时间】:2013-02-01 00:47:38
【问题描述】:

我在同一天晚上的第二个帖子,道歉!

我有一个 XML 文件,其中包含各种软件条目的几位信息(如下所示的 sn-p)。

<software>
    <software_entry
    name="Adobe Acrobat X Standard"
    path="Applications\Acrobat\Acrobat X Standard\AcroStan.msi"
    type="msi"
    switches="/qn ALLUSERS=1"
    />
    <software_entry
    name="Adobe Acrobat X Professional"
    path="Applications\Acrobat\Acrobat X Pro\AcroPro.msi"
    type="msi"
    switches="/qn ALLUSERS=1"
    />
</software>

我正在使用 LINQ to XML 加载 XML 文件。在我的应用程序中,我有两个列表框,第一个列表框 (listBox1) 填充了此 XML 文件中每个条目的名称。然后用户可以将这些条目从一个列表框移动到另一个列表框 (listBox2)。

我正在尝试编写一个方法来遍历 listBox2 中的每个条目,在 XML 文件中找到匹配的条目,然后检索该条目的所有其他属性。例如,用户已将 Adob​​e Acrobat X Professional 添加到第二个列表框。该方法应该查看 XML 文件,找到与列表中的内容匹配的属性“名称”的条目,并写入其他数据(路径、类型、开关)或将它们设置为字符串的值。到目前为止,经过多次调整,我取得的成就很少。我设法得到了一些非常乱码的输出。

到目前为止我的代码如下。

private void writeBatchFile()
{
    // Get temp folder location
    string tempPath = Path.GetTempPath();

    // Create batch file
    using (StreamWriter writer = File.CreateText(tempPath + @"\swInstaller.cmd"))
    {
        // Loop through entries in listBox2
        foreach (string item in listBox2.Items)
        {
            var dataFromXML = from data in document.Descendants("software_entry")
                               where data.Element("name").Value == item
                               select new
                               {
                                   fileName = (string) data.Element("name"),
                                   filePath = (string) data.Element("path"),
                                   fileType = (string) data.Element("type"),
                                   fileSwitches = (string) data.Element("switches")
                               };

            // Write to batch file
            writer.WriteLine(fileName + filePath + fileType + fileSwitches);
        }
    }
}

在代码中,我为 listBox2 中的文本值创建了一个 foreach 循环。然后,我尝试在 XML 中查找该值等于“名称”值的位置,然后在找到时将该值的其他属性设置为这些字符串(路径为 filePath,类型为 fileType 等)。最后,我试图将这些字符串写入文本文件。通过这种特殊的尝试,我得到的只是 5 个空白行。谁能指出我做错了什么的正确方向?

非常感谢。

【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:

    您正在尝试读取 XML 属性,而不是元素。请改用Attribute 方法:

    var dataFromXML = Enumerable.Single
    (
        from data in document.Descendants("software_entry")
        where data.Attribute("name").Value == item
        select new
        {
            fileName = data.Attribute("name").Value,
            filePath = data.Attribute("path").Value,
            fileType = data.Attribute("type").Value,
            fileSwitches = data.Attribute("switches").Value
        }
    );
    
    writer.WriteLine(dataFromXML.fileName);
    

    您还需要进行这些额外的更改:

    • 使用Enumerable.Single() 表示您只希望1 个元素与给定名称匹配。否则dataFromXML 可能包含多个元素,您需要使用循环或索引来访问它们。
    • 您需要访问fileName 和其他变量作为dataFromXML 变量的成员。没有自己的局部变量,这就是您收到您提到的错误消息的原因。
    • 您不需要强制转换为 string,因为从 XML 属性读取的值默认情况下已经是字符串。

    【讨论】:

    • 感谢您的回复。那么我的循环在逻辑上真的有效吗?我得到的文件名、文件路径等在当前上下文中不存在。您/我编写的代码是否实际上将它们设置为字符串,还是我还需要做其他事情?我已经对我的代码进行了一些编辑 - 我只想在测试阶段将其全部写入一行,稍后我将对其进行格式化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多