C# 字段初始值设定项在创建XML数据时不能引用非静态字段、方法或属性
我有以下代码。我想使用XML元素在SQL数据库中插入数据用户名和密码。但其显示错误: 字段初始值设定项不能引用非静态字段、方法或属性 我还提到了我的预期产出 我已尝试以以下方式得出结果:C# 字段初始值设定项在创建XML数据时不能引用非静态字段、方法或属性,c#,asp.net,C#,Asp.net,我有以下代码。我想使用XML元素在SQL数据库中插入数据用户名和密码。但其显示错误: 字段初始值设定项不能引用非静态字段、方法或属性 我还提到了我的预期产出 我已尝试以以下方式得出结果: XDocument doc = new XDocument(); XElement ele = new XElement("Element", new XElement("UserId", strUserName), new XElement("Password", strPassword)); 但我无法访问
XDocument doc = new XDocument();
XElement ele = new XElement("Element", new XElement("UserId", strUserName), new XElement("Password", strPassword));
但我无法访问strUserName和strPassword.:
假设我传递了硬编码的用户名和密码,它工作正常
XDocument doc = new XDocument();
XElement ele = new XElement("Element", new XElement("UserId", "admin"), new XElement("Password", "admin"));
在函数中移动逻辑。谢谢@mjwills