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

从c#代码更改dtsConfig文件中的值,c#,xml,ssis,C#,Xml,Ssis,我有一个dtsConfig文件,其中包含ssis包变量User::Quote\u ID: <Configuration ConfiguredType="Property" Path="\Package.Variables[User::Quote_ID].Properties[Value]" ValueType="String"><ConfiguredValue>77777</ConfiguredValue></Configuration> 它

我有一个dtsConfig文件,其中包含ssis包变量User::Quote\u ID:

<Configuration ConfiguredType="Property" 
Path="\Package.Variables[User::Quote_ID].Properties[Value]" 
ValueType="String"><ConfiguredValue>77777</ConfiguredValue></Configuration>
它在代码的第三行(XmlNode…)上给出了一个错误:

“User::Quote_ID”具有无效令牌

怎么了?

节点名称是“配置”。其中有一个名为“Path”的属性,其值为“\Package.Variables[User::Quote\u 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\u ID].Properties[Value]
更改为
\Package.Variables[newValue].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;
更新的示例

这将用
quote\u ID.ToString()
替换第一个节点的
7777777
值(我假设
5555555
在其中),其中“路径”是
\Package.Variables[User::quote\u 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;

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

XmlNodeList listOfConfigurationNodes = xDoc.SelectNodes("Configuration");
我提出:

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

这段代码适用于我的特殊情况。非常感谢

您熟悉XPath吗?你可以用它来做这件事。。快速谷歌搜索,但我不想用变量值替换变量名。我想将变量值替换为新值,例如77777到55555。还有一个问题:我有许多名为“配置”的节点——每个变量一个。程序如何知道我想要更改这个特定变量“User::Quote_ID”的值?我已经包含了一个更新的示例,解释了如何执行此操作。太棒了!另一个可以直接“跳转”到所需节点的选项是使用双斜杠,如
“//配置”
,尽管我倾向于使用这种方式,因为当您将更多路径显式化时,风险可能更小。您还可以将代码缩减为一行,然后说
XmlNodeList-listOfConfigurationNodes=XMLOuterNode.SelectNodes(“DTSConfiguration/Configuration”)
.Correction:
XmlNodeList-listOfConfigurationNodes=xDoc.SelectNodes(“dtscoconfiguration/Configuration”)