C#XElement加载获取元素值

时间:2015-06-08 17:51:46

标签: c# linq-to-xml xelement

我有以下XML但很难获取DisplayName文本,它一直说它是一个空值:

<?xml version="1.0" encoding="utf-8"?>
<Package Test="Value">
<Identity Name="Reserved" Publisher="CN=Reserved" Version="0.0.0.0" />
<Properties>
    <DisplayName>Application Name</DisplayName>
    <PublisherDisplayName>Reserved</PublisherDisplayName>
    <Description>Reserved</Description>
    <Logo>Reserved.jpeg</Logo>
</Properties>
</Package>

我使用了以下

XElement Manifest = XElement.Load(@"c:\temp\file.xml");
var ins = Manifest.Attribute("Test").Value.ToString();

var PackageName = Manifest.Element("Properties").Attribute("DisplayName").Value; // doesn't work

如何获取DisplayName文本?

在Powershell中,它很容易,但我很难用C#做同样的事情

[xml]$manifest = gc "C:\temp\file.xml"
$manifest.Package.Test
$DisplayName = $manifest.Package.Properties.DisplayName

1 个答案:

答案 0 :(得分:0)

var manifest = XElement.Load(@"C:\temp\log\foo.txt");
var packageName = manifest
    .Element("Properties")
    .Elements()
    .First(x => x.Name == "DisplayName")
    .Value;