【问题标题】:Unassigned local variable/ alter XML Format未分配的局部变量/更改 XML 格式
【发布时间】:2015-10-22 19:30:11
【问题描述】:

我的程序接受用户输入并使用它来创建查询。然后,该查询的结果将根据 XElements 的选择放入一个 XML 文件中。我有一个字符串staff,我将其设置为staffType 的值。

用查询代码的 sn-p 声明人员的位置:

        using (
            var conn = new SqlConnection("Server=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Database=KUDERDEV;User ID=xxxxxxxxxxxxxxxxxx;Password= xxxxxxxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
            )
        {
            conn.Open();
            bool quit = false;
            string choice;
            string staff;
            SqlCommand cmd = new SqlCommand();

            while (!quit)
            {
                Console.WriteLine("Sort by staffType: K12Staff, psStaff, WFStaff or none?");
                string staffType = Console.ReadLine();
                staff = staffType;
                if (staffType == "K12Staff")
                {
                    Console.WriteLine("Sort by code, date, both, or none?");
                    choice = Console.ReadLine();
                    switch (choice)
                    {
                        case "code":
                            Console.WriteLine("Sort by code1 or code2?");
                            string codeCol = Console.ReadLine();
                            Console.WriteLine("Enter desired code");
                            string code = Console.ReadLine();
                            cmd = new SqlCommand("SELECT * FROM Staff WHERE (Staff." + @codeCol + "='" + @code + "') AND staffType ='" + @staffType + "' FOR XML PATH('staff'), ROOT('k12Staff')", conn);
                            quit = true;
                            staff = staffType;
                            break;

当查询字符串完成后,我进入另一个 using 语句而不关闭第一个以写入 XML 文件。在这里,我想根据所选择的人员类型更改 XML (XElement) 的格式。

编写 XML 文件 sn-p:

        using (cmd)
        {
            using (var reader = cmd.ExecuteXmlReader())
            {
                var doc = XDocument.Load(reader);
                string path = @"Staff." + DateTime.Now.ToString("yyyyMMdd") + ".xml";
                using (var writer = new StreamWriter(path))
                {
                    //if (staff == "k12Staff")
                    XNamespace ns = "http://specification.sifassociation.org/Implementation/na/3.2/html/CEDS/K12/K12_k12Staff.html";
                    var root = new XElement(ns + "k12Staff");

                    foreach (var d in doc.Descendants("staff"))
                    {
                        root.Add(new XElement(ns + "staff",
                                    new XElement(ns + "identity",
                                        new XElement(ns + "name",
                                            new XElement(ns + "firstName", first),
                                            new XElement(ns + "lastName", last)
                                            )
                                        ),
                                    new XElement(ns + "employment",
                                        new XElement(ns + "positionTitle", position)
                                            ),
                                    new XElement(ns + "assignment",
                                        new XElement(ns + "leaID", leaID),
                                        new XElement(ns + "schoolID", schoolID)
                                                ),
                                    new XElement(ns + "contact",
                                        new XElement(ns + "phoneNumberList",
                                            new XElement(ns + "number", phone),
                                            new XElement(ns + "phoneNumberIndicator", indicator)
                                        ),
                                        new XElement(ns + "emailList",
                                            new XElement(ns + "email", email)
                                            )
                                            ),
                                    new XElement(ns + "delete", delete)

然后,如果 staffType 是不同的东西,例如“psStaff”,那么我会更改 XML (XElement) 的格式以具有不同的名称、位置等。

所以会是这样的:

 if(staff == "k12Staff"){
         format xml here...
 }
 else if (staff == "psStaff"){
         format xml here....
 }

等等等等。

我的问题:

在前面的例子中,我的代码有if(staff == "k12Staff"),但我被告知它是一个分配的局部变量。我曾尝试在 using 语句之外声明员工,并尝试在类似 using(staff) 的 using 语句中使用 staff,这就是我使用 cmd 变量的方式。为什么我的程序能识别cmd 而不能识别staff

【问题讨论】:

  • 你的标题是“unassigned-local-variable。我刚刚给出了答案。但后来我再次阅读了你的问题,在你的文字中你写了一个分配变量有问题吗?所以请澄清一下。我希望我的答案不完全放在一边……
  • 嗨,布兰登,如果我的回答对您有帮助,请您投票并将其标记为已接受的答案,真是太好了。谢谢!

标签: c# sql xml using-statement unassigned-variable


【解决方案1】:

您没有发布整个代码,因此无法说明您的变量 staff 应该在哪里获得值以及为什么没有。

未赋值表示:存在但从未获得值...

尝试以下操作:在声明变量的地方更改:

    string staff="defaultValue";

在你处理变量内容的地方改变这个:

 if(staff == "k12Staff"){
         format xml here...
 }
 else if (staff == "psStaff"){
         format xml here....
 }
 else if (staff == "defaultValue"){
      //What ever is to be done if all attempts to set "staff" did not work
      //You must set a stop mark before your `while (!quit)` and step through.
      //There is at least one situation, where `staff` is not set to any value...
      //If there's a bug you must fix this, if this is allowed to happen, solve it here...
 }

【讨论】:

  • 我不想发布整个代码,因为它有将近 400 行。第一个 sn-p 一遍又一遍地重复,但变量不同。如果你愿意,我可以把整个代码发给你。
  • @BrandonLumsden,你有没有尝试我的建议? “员工”有什么价值?它是“默认值”吗?如果是,您将不得不逐步查找,为什么您的“员工”没有设置为正确的值...
  • 看起来我只需要用一个值而不是 string staff; 来声明员工。疯狂地花了 2 天时间寻找这么小的东西。谢谢。
  • @BrandonLumsden,很高兴看到我可以帮助您解决这个问题!编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-17
  • 2013-10-18
  • 2015-11-09
  • 2012-05-18
相关资源
最近更新 更多