0

I have an XML file as follows. I would like to extract the Version number from this file. I tried XML parsing. But, that is for node values only. I am able to get this file as string as follows. var doc = XDocument.Load("WMAppManifest.xml");

<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2012/deployment" AppPlatformVersion="8.0">
  <DefaultLanguage xmlns="" code="en-US" />
  <App xmlns="" ProductID="{a3f55b1e-c183-4645-9b19-87a41a206978}" Title="sometitle" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="M-Files author" BitsPerPixel="32" Description="Apache Cordova for Windows Phone" Publisher="CordovaExample" PublisherID="{b93a0d8e-5aa9-4d9b-b232-17e2d852e779}">
    <IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath>
   </App>
</Deployment>
Ramesh
  • 392
  • 1
  • 12
  • 39
  • 3
    hint: do *NOT* use regex! (see the following http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ColinE Jan 06 '14 at 08:41
  • Which version? The one in ` – Anton Tykhyy Jan 06 '14 at 08:42
  • 1
    Christ on a bike sir. There are 3 versions in here and you state "extract the Version number from this **file**" so everyone gives you a file version. Please be more specific in future. – Gusdor Jan 06 '14 at 08:48

1 Answers1

4

You can access the XML declaration node as follows:

XmlDeclaration declaration = doc.ChildNodes
                                .OfType<XmlDeclaration>()
                                .FirstOrDefault();

You can then read the value of declaration.Version.

Or, if you are after the 'app' version attribute in the XML document itself, try the following

string version = doc.Descendants("App")
                    .Single()
                    .Attribute("Version").Value
ColinE
  • 68,894
  • 15
  • 164
  • 232