Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XML解析器从DTSX文件获取XML元素值_C#_Vb.net_Ssis_Xml Parsing_Etl - Fatal编程技术网

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

C# 使用XML解析器从DTSX文件获取XML元素值,c#,vb.net,ssis,xml-parsing,etl,C#,Vb.net,Ssis,Xml Parsing,Etl,dtsx(SSIS包文件)是一个Xml文件。它包含一个名为PackageFormatVersion的元素(哪个版本的SSDT与此包相关) 但我认为实现这一点的推荐方法是使用XML解析器,我不知道如何实现它。有什么帮助吗 我接受C#的答案 包Xml看起来像: <?xml version="1.0"?> <DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts" DTS:refId="Package" DTS:Crea

dtsx(SSIS包文件)是一个Xml文件。它包含一个名为
PackageFormatVersion
的元素(哪个版本的SSDT与此包相关)

但我认为实现这一点的推荐方法是使用
XML解析器
,我不知道如何实现它。有什么帮助吗

我接受C#的答案

包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 ...

8.

最后我找到了解决办法

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)

我从这个问题开始

为什么使用XML解析器是“推荐的方法”?我喜欢你所做的。正则表达式快速、直观,不需要对完整XML文档的嵌套结构进行详细解析。如果使用XML解析器,您将绑定到DTS包的近似模式(可以在不同版本之间更改)。虽然您所追求的属性当前确实存在于顶级节点中,但regex仍然可能是一种更精简、更快的方法。记住您的实际需求,让这些需求的最佳解决方案胜出@如果向元素添加新属性,或者添加空白换行符,ryancdotnet正则表达式将失败。而xml解析器可以通过名称搜索特定标记。然后得到它的值。此外,如果ssis包含名为
PackageFormatVersion
的变量,则正则表达式可能会失败(用户可能不知道此属性,而将此名称用作变量)。此外,我认为,当一切都是关于结构良好的数据时,regex不是推荐的方法。
<?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 ...
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)