C# XElement Load get Element Value

836 views Asked by At

I have the following XML but struggling to get the DisplayName text, it keeps saying its a null value:

<?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>

I've used the following

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

How do I get the DisplayName text?

In Powershell its really easy but I'm struggling to do the same with C#

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

There are 1 answers

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