简体   繁体   中英

Get an XML Element value from a DTSX File Using an XML Parser

The dtsx (SSIS package file) is an Xml file. it contains an element named PackageFormatVersion (Which version of SSDT are related to this package)

 <DTS:Property
DTS:Name="PackageFormatVersion">8</DTS:Property>

I have written a Vb.net script to retrieve this element value using RegEx using the following expression (and it is working fine)

  Dim strA As String = Regex.Match(strContent, "(?<=""PackageFormatVersion"">)(.*)(?=</DTS:Property>)", RegexOptions.Singleline).Value

But i think that the recommended way to achieve this is by using an XML Parser , the way that i didn't know how to achieve it. Any Help?

I accept answers in C#

Package Xml Looks like:

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"
 DTS:refId="Package"
 DTS:CreationDate="3/26/2017 11:44:38 PM"
 DTS:CreationName="Microsoft.Package"
 DTS:CreatorComputerName="MyComputer"
 DTS:CreatorName="MyComputer\Admin"
 DTS:DTSID="{384605BC-FC77-4506-B409-C1EE9B21BAE2}"
 DTS:ExecutableType="Microsoft.Package"
 DTS:LastModifiedProductVersion="13.0.4001.0"
 DTS:LocaleID="1033"
 DTS:ObjectName="Package"
 DTS:PackageType="5"
 DTS:VersionBuild="2"
 DTS:VersionGUID="{66D08BFA-6426-4123-99F7-6E655B79AF6D}">
 <DTS:Property
 DTS:Name="PackageFormatVersion">8</DTS:Property>
 <DTS:ConnectionManagers>
 <DTS:ConnectionManager ...

Finally i found the solution

Dim strA As String = ""

Dim xml = XDocument.Load(strFile)

Dim ns As XNamespace = "www.microsoft.com/SqlServer/Dts"
Dim man As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
man.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts")

If Not xml.Root Is Nothing AndAlso
   Not xml.Root.Descendants(ns + "Property").Attributes(ns + "Name") Is Nothing AndAlso
   xml.Root.Descendants(ns + "Property").Attributes(ns + "Name").Where(Function(x) x.Value = "PackageFormatVersion").Count > 0 Then

   strA = xml.Root.Descendants(ns + "Property").Attributes(ns + "Name").Where(Function(x) x.Value = "PackageFormatVersion").FirstOrDefault.Parent.Value


End If

Msgbox(strA)

i started from this question Parse SSIS .xml source to retrieve table mappings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM