public void GetLocalFilePath() { ///Xml转json XElement xele = XElement.Load(Path.Combine(localUserInfoFilepath, FileName)); var item = (from ele in xele.DescendantsAndSelf("User") select ele).ToList(); if (item.Count > 0) //存在用户 { XmlDocument document = new XmlDocument(); document.LoadXml(xele.ToString()); string str = JsonConvert.SerializeXmlNode(document); JObject json = (JObject)JsonConvert.DeserializeObject(str); //判断是否存在“[”“]” 来判断是否为多用户 正则匹配 Regex reg = new Regex(@"\[[^\[\]]+\]"); MatchCollection Users = reg.Matches(str); bool UsersCount = Users.Count > 0; //大于0 至少有两个用户 等于0 只存在一个用户 int index = 0; if (UsersCount) { for (int i = 0; i < json["Root"]["User"].Count(); i++) { var cc = json["Root"]["User"][i]; LocalUserInfo.Add(new LocalUserInfo() { Index = index + 1, Phone = cc["Phone"].ToString(), Pwd = cc["Pwd"].ToString(), }); index++; } } else { var cc = json["Root"]["User"]; LocalUserInfo.Add(new LocalUserInfo() { Index = index + 1, Phone = cc["Phone"].ToString(), Pwd = cc["Pwd"].ToString(), }); } } }