0

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
Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
  • `DisplayName` is element not attribute. `Manifest.Element("Properties").Element("DisplayName").Value` – user4003407 Jun 08 '15 at 17:56
  • @ PetSerAl, Why don't you write the answer? :) – FoggyFinder Jun 08 '15 at 18:01
  • Thanks for the reply, that returns as a null value – user3558247 Jun 08 '15 at 18:03
  • Why?: https://dotnetfiddle.net/Zsl8id – FoggyFinder Jun 08 '15 at 18:10
  • Thanks for the reply Foggy Finder, I've seen a lot of people using XDocument.parse but I'm trying to use the XElement.load("pathtoxml") Which is the best way to load XML? – user3558247 Jun 08 '15 at 18:16
  • Parse - only for xml as string. Load - Load from file, network, etc. Msdn: https://msdn.microsoft.com/en-us/library/bb343181(v=vs.110).aspx (Load) https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse(v=vs.110).aspx (Parse). I used .Parse for illustration purposes only. Sorry for my English – FoggyFinder Jun 08 '15 at 18:45
  • Thanks for the clarification. Can you show a working example with XElement.Load? Thanks Dave – user3558247 Jun 08 '15 at 19:39
  • Replace XDocument.Parse (xml); on XDocument.Load (@ "c: \ temp \ file.xml"); . Attention, .xml from the start message is not valid. You can check, for example, here: http://validator.w3.org/#validate_by_input+with_options. You have not closed the tag - Package. See how I corrected xml in the previous link – FoggyFinder Jun 08 '15 at 21:30

1 Answers1

0
var manifest = XElement.Load(@"C:\temp\log\foo.txt");
var packageName = manifest
    .Element("Properties")
    .Elements()
    .First(x => x.Name == "DisplayName")
    .Value;
Handcraftsman
  • 6,863
  • 2
  • 40
  • 33