未分配的局部变量/ alter XML格式

时间:2015-10-22 19:30:12

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

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

宣布工作人员w /查询代码片段的地方:

        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文件的语句。在这里,我想根据选择的staffType更改XML(XElement)的格式。

编写XML文件摘要:

        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语句之外声明工作人员,并尝试在staff之类的using语句中使用using(staff),这就是我使用cmd变量的方式。我的程序如何识别cmd但不识别staff

1 个答案:

答案 0 :(得分: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...
 }