【问题标题】:Changing value in dtsConfig file from c# code从 C# 代码更改 dtsConfig 文件中的值
【发布时间】:2017-11-27 21:52:22
【问题描述】:

我有一个 dtsConfig 文件,其中包含 ssis 包变量 User::Quote_ID: 的配置:

<Configuration ConfiguredType="Property" 
Path="\Package.Variables[User::Quote_ID].Properties[Value]" 
ValueType="String"><ConfiguredValue>77777</ConfiguredValue></Configuration>

我想从 c# 代码中更改这个值:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
XmlNode xFile = xDoc.SelectSingleNode("User::Quote_ID");               
xFile.Value = quote_ID.ToString();
xDoc.Save(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
xDoc = null;

它在我的代码的第三行 (XmlNode...) 上给了我一个错误:

‘User::Quote_ID’的令牌无效

怎么了?

【问题讨论】:

  • 您熟悉 XPath 吗?你可以用那个来做这个..做一个快速的谷歌搜索

标签: c# xml ssis


【解决方案1】:

克里斯!您的代码对我帮助很大!就我而言,它不起作用。我在调试模式下运行应用程序,我可以看到 xDoc.Load... 打开了正确的文件,但它没有执行 foreach 循环。属性 listOfConfigurationNodes 有 Count = 0。我再次检查了我的 xml 文件,发现它有外部节点和该外部节点内的所有节点。所以我改变了你的代码

XmlNodeList listOfConfigurationNodes = xDoc.SelectNodes("Configuration");

我做了:

XmlNode XMLOuterNode = xDoc.SelectSingleNode("DTSConfiguration");
XmlNodeList listOfConfigurationNodes = XMLOuterNode.SelectNodes("Configuration");

此代码适用于我的特定情况。非常感谢!!!

【讨论】:

  • 太棒了!您可以用来直接“跳转”到您想要的节点的另一个选项是使用双斜杠,例如"//Configuration",尽管我更喜欢您这样做的方式,因为当您明确路径时它可能会降低风险。您也可以将代码缩减为一行并说XmlNodeList listOfConfigurationNodes = XMLOuterNode.SelectNodes("DTSConfiguration/Configuration");
  • 更正:XmlNodeList listOfConfigurationNodes = xDoc.SelectNodes("DTSConfiguration/Configuration");.
【解决方案2】:

节点名称为“配置”。其中,您有一个名为“Path”的属性,其值为“\Package.Variables[User::Quote_ID].Properties[Value]”。

我不确定您需要在代码中做什么,但这里有一个示例,说明了更改该值的一种方法:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
XmlNode xFile = xDoc.SelectSingleNode("Configuration");
xFile.Attributes["Path"].Value = xFile.Attributes["Path"].Value.Replace("User::Quote_ID", "newValue");
xDoc.Save(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
xDoc = null;

上面的例子会将\Package.Variables[User::Quote_ID].Properties[Value]改为\Package.Variables[newValue].Properties[Value]

更新示例

这将用quote_ID.ToString() 替换77777 值(我假设55555 在那里)用于第一个节点(对于所有节点,删除break;),其中“路径”是@987654328 @。

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
XmlNodeList listOfConfigurationNodes = xDoc.SelectNodes("Configuration");
foreach (XmlNode node in listOfConfigurationNodes)
{
    if (node.Attributes["Path"].Value == @"\Package.Variables[User::Quote_ID].Properties[Value]")
    {
        node.SelectSingleNode("ConfiguredValue").InnerText = quote_ID.ToString();
        break;
    }
}
xDoc.Save(@"\\MyServer\DataFiles$\...\MyConfigFile.dtsConfig");
xDoc = null;

【讨论】:

  • 但我不想用变量值替换变量名。我想将变量值替换为新值,例如 77777 到 55555。还有另一个问题:我有许多名为“配置”的节点 - 每个变量一个。程序如何知道我想更改这个特定变量“User::Quote_ID”的值?
  • 我已经包含了一个更新的示例来解释如何执行此操作。
猜你喜欢
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 2021-10-06
  • 2015-01-15
  • 2014-07-03
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多