从c#代码更改dtsConfig文件中的值

时间:2017-11-27 21:52:23

标签: c# xml ssis

我有一个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'的标记无效

有什么问题?

2 个答案:

答案 0 :(得分:1)

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

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

我做了:

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

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

答案 1 :(得分:0)

节点名称为“Configuration”。在其中,您有一个名为“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]

更新了示例

这将替换77777quote_ID.ToString()(我假设55555在那里)第一个节点(对于所有节点,删除break;)其中“路径”为\Package.Variables[User::Quote_ID].Properties[Value]

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;
相关问题