【发布时间】:2010-11-13 12:32:18
【问题描述】:
我正在尝试从下面附加的 xml 文件中查询出属性值。具体来说,我正在尝试获取“ColumnNullRatioProfile”节点下的“Column”元素节点中的Name、SqlDbType 等属性值。 xml 输出文件是 SQL Server 2008 SSIS DataProfiler 任务的一部分。我的目标是使用 Powershell 创建一个 CSV 文件,其中包含可以加载到 Excel 工作簿中的选定属性。
但是,我尝试了几种方法(请参阅下面的方法 1 和方法 2 中的一些方法。我无法使其工作。有什么建议吗?
#Save as t.xml on C:\
#-----------------------
<?xml version="1.0" ?>
<DataProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/sqlserver/2008/DataDebugger/">
<DataSources>
<DtsDataSource ID="{45277997-59B4-4A95-909E-7804F0761FA1}" Name="DatabaseConn">
<DtsConnectionManagerID>DatabaseConn</DtsConnectionManagerID>
</DtsDataSource>
<DtsDataSource ID="{BAEE1FCA-E5A2-4C3C-A1B6-100B3B681397}" Name="Table.xml">
<DtsConnectionManagerID>Table.xml</DtsConnectionManagerID>
</DtsDataSource>
</DataSources>
<DataProfileOutput>
<Profiles>
<ColumnNullRatioProfile ProfileRequestID="NullRatioReq" IsExact="true">
<DataSourceID>{45877997-59B4-4A95-909E-7804F0761FA1}</DataSourceID>
<Table DataSource="XVRTFD0585\SQL905" Database="BusinessData" Schema="General" Table="Email_Notifications_Lookup" RowCount="-1" />
<Column Name="EmailURL_ID" SqlDbType="Int" MaxLength="0" Precision="10" Scale="0" LCID="-1" CodePage="0" IsNullable="false" StringCompareOptions="0" />
<NullCount>0</NullCount>
</ColumnNullRatioProfile>
<ColumnNullRatioProfile ProfileRequestID="NullRatioReq1" IsExact="true">
<DataSourceID>{45CC99B2-E396-4CFA-A1F5-4E703F04E9E7}</DataSourceID>
<Table DataSource="XVRTFD0585\SQL905" Database="BusinessData" Schema="General" Table="LOOKUP_CODES" RowCount="5979114" />
<Column Name="TRANS_ID" SqlDbType="Decimal" MaxLength="0" Precision="9" Scale="0" LCID="-1" CodePage="0" IsNullable="true" StringCompareOptions="0" />
<NullCount>5979114</NullCount>
</ColumnNullRatioProfile>
</Profiles>
</DataProfileOutput>
</DataProfile>
#Method 1
#--------
$uri="C:\t.xml"
$xDoc = [xml](Get-Content $uri )
$XDoc.DataProfile.DataProfileOutput.Profiles.ColumnNullRatioProfile.Column | select Name
#Method 2 (Using LINQ)
#--------
$uri="C:\t.xml"
[Reflection.Assembly]::LoadWithPartialName(”System.Xml.Linq”) | Out-Null
$XDoc = [System.Xml.Linq.XDocument]::Load($uri)
$XDoc.Descendants(“ColumnNullRatioProfile”) | ForEach {$_.Element("Column”).GetAttribute("Name").Value}
【问题讨论】:
标签: sql-server xml sql-server-2008 powershell ssis