简体   繁体   English

使用 XML 解析器从 DTSX 文件中获取 XML 元素值

[英]Get an XML Element value from a DTSX File Using an XML Parser

The dtsx (SSIS package file) is an Xml file. dtsx(SSIS 包文件)是一个 Xml 文件。 it contains an element named PackageFormatVersion (Which version of SSDT are related to this package)它包含一个名为PackageFormatVersion的元素(SSDT 的哪个版本与此包相关)

 <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)我已经编写了一个 Vb.net 脚本来使用以下表达式使用RegEx检索此元素值(并且它工作正常)

  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.但我认为实现这一点的推荐方法是使用XML Parser ,我不知道如何实现它。 Any Help?任何帮助?

I accept answers in C#我接受 C# 中的答案

Package Xml Looks like:包装 XML 看起来像:

<?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我从这个问题开始解析 SSIS .xml 源来检索表映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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