private readonly string _roleXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Roles.xml");
 private readonly string _companyXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Companys.xml");        public StringBuilder GetAllRoles()
        {
            StringBuilder reVal = new StringBuilder(); //定义一个变量
            XDocument xDoc = XDocument.Load(_roleXmlPath);
            var roles = from x in xDoc.Descendants("role") select x;
            reVal.Append("[");
            foreach (XElement e in roles)
            {
                if (reVal.Length > 1) { reVal.Append(","); }
                reVal.Append("{\"text\":\"" + e.Element("text").Value + "\",\"value\":\"" + e.Element("value").Value + "\"}");
            }
            reVal.Append("]");
            return reVal;
        }这个看不是很懂,求专业人士给分析一下,最好能写一下注释,谢谢

解决方案 »

  1.   

    private readonly string _roleXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Roles.xml");//路径常量
     private readonly string _companyXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Companys.xml");//路径常量  public StringBuilder GetAllRoles()
      {
      StringBuilder reVal = new StringBuilder(); //定义一个变量
      XDocument xDoc = XDocument.Load(_roleXmlPath);//加载XML
      var roles = from x in xDoc.Descendants("role") select x;//延迟读取XML信息
      reVal.Append("[");//将读取出的XML信息以JSON的格式存储到StringBuilder字符串变量reVal中
      foreach (XElement e in roles)
      {
      if (reVal.Length > 1) { reVal.Append(","); }
      reVal.Append("{\"text\":\"" + e.Element("text").Value + "\",\"value\":\"" + e.Element("value").Value + "\"}");
      }
      reVal.Append("]");
      return reVal;
      }
      

  2.   

    这两句不是很懂,求详细解释 ,谢谢
    var roles = from x in xDoc.Descendants("role") select x;//延迟读取XML信息foreach (XElement e in roles)
      {
      if (reVal.Length > 1) { reVal.Append(","); }
      reVal.Append("{\"text\":\"" + e.Element("text").Value + "\",\"value\":\"" + e.Element("value").Value + "\"}");
      }
      

  3.   

    private readonly string _roleXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Roles.xml");
     private readonly string _companyXmlPath = HttpContext.Current.Server.MapPath("~/App_XMLs/Companys.xml");  //定义两个只读字符串变量保存两个xml文件的绝对路径  public StringBuilder GetAllRoles()
      {
      StringBuilder reVal = new StringBuilder(); //定义一个字符串变量reVal
      XDocument xDoc = XDocument.Load(_roleXmlPath); //读取xml数据
      var roles = from x in xDoc.Descendants("role") select x;  //从xml数据中查找role的所有节点
      reVal.Append("[");  //reVal加上字符"["  foreach (XElement e in roles)    //循环role这个xml节点,
      {
      if (reVal.Length > 1) { reVal.Append(","); }  //如果reVal长度大于1则加一个字符,
      reVal.Append("{\"text\":\"" + e.Element("text").Value + "\",\"value\":\"" + e.Element("value").Value + "\"}");  //regVal循环添加字符串格式是这样的  {"text":"role节点下text的值","value":"role节点下value的值"}
      }
      reVal.Append("]");
      return reVal;
      }
      

  4.   

    var roles = from x in xDoc.Descendants("role") select x; //从xml数据中查找role的所有节点
    那个X是干嘛用的啊
      

  5.   

    x在from后面定义了啊 x in xDoc.Descendants("role"),楼主先去看看xml操作的基础知识吧