2015年10月15日 星期四

"在根層次的資料無效,行1,位置1" 解法,StreamReader reader = new StreamReader(Notifile.CreateReadOnlyStream(), new UTF8Encoding(false))


//除此之外最好的方法就是將 XML檔案再記憶體的狀態檢查,通常可以發現問題

public string XMLFileStatusType(Notification Notifile)
        {
            try
            {
           
                string xmlString = "";
                ///錯誤訊息: 在根層次的資料無效。 第 1 行,位置 1 解法如下: new UTF8Encoding(false)
                using (StreamReader reader = new StreamReader(Notifile.CreateReadOnlyStream(), new UTF8Encoding(false)))
                    xmlString = reader.ReadToEnd();
                using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
                {


                    reader.ReadToFollowing("status");//---這行將會出錯


                    if (reader.NodeType == XmlNodeType.None)
                        return "NoneType";

                    switch (reader.ReadElementContentAsString())
                    {
                        case "Exercise":
                            return "Exercise";
                    }

                    return "None";
                }
            }
            catch
            {
                return "None";
            }
        }