问题如下:
我的XML文件有2个节点即 PatchesName 和 PatchesKb ,我写的是2个循环(就是通过动态获取值,写入XML文件)。但是问题出现了如果在 arrayPatchesOffice数组有相应的值对应时显示出来的XML结果是没错的,如果arrayPatchesOffice没有相应的值时,则写入XML的值就变成重复的。我意思是想当arrayPatchesOffice数组中的值都取完的情况之下不写入该节点PatchesKb只有arrayPatchesOffice数组中有值时才写入到XML 文件中,当arrayPatches数组中的值要全部写入XML中。
    for (int z = 0; z < arrayPatches.Count; z++) //arrayPatches外部ArrayList数组里面是获取的动态值          {                for (int j = 0; j < arrayPatchesOffice.Count;j++) //arrayPatchesOffice外部ArrayList数组里面是获取的动态值              {
                  XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName");
                  xesub4_3_1.InnerText = arrayPatches[z].ToString();
                  xesub4_3.AppendChild(xesub4_3_1);
               
                  XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb");
                  xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString();
                  xesub4_3.AppendChild(xesub4_3_2);
 
             }
          }
请大侠们指教小弟,谢谢!!!

解决方案 »

  1.   

    我意思是想当arrayPatchesOffice数组中的值都取完的情况之下不写入该节点PatchesKb只有arrayPatchesOffice数组中有值时才写入到XML 文件中,当arrayPatches数组中的值要全部写入XML中。 没看明白你要表达什么意思,我理解的就是,你需要一句简单的判断就成了....你这句话,似乎缺少了标点符号,读不通顺啊
      

  2.   

    我提个思路:先把arrayPatchesOffice中的数据全删了,在重新载入
      

  3.   

    问题描述得不够清楚
    for (int z = 0; z < arrayPatches.Count; z++)    
     {  
    XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName"); 
                      xesub4_3_1.InnerText = arrayPatches[z].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_1);               for (int j = 0; j < arrayPatchesOffice.Count;j++) 
                { 
                                    
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb"); 
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_2);             } 
              } 
      

  4.   

    问题如下: 
    我的XML文件有2个节点即 PatchesName 和 PatchesKb ,我写的是2个循环(就是通过动态获取值,写入XML文件)。但是问题出现了如果在 arrayPatchesOffice数组有相应的值对应时显示出来的XML结果是没错的,如果arrayPatchesOffice没有相应的值时,则写入XML的值就变成重复的。我意思是想:当arrayPatchesOffice数组中的值都取完的情况之下,不写入该节点PatchesKb。只有arrayPatchesOffice数组中有值时才写入到XML 文件中,但是最后arrayPatches数组中的值要全部写入XML中。 
        for (int z = 0; z < arrayPatches.Count; z++) //arrayPatches外部ArrayList数组里面是获取的动态值          {                for (int j = 0; j < arrayPatchesOffice.Count;j++) //arrayPatchesOffice外部ArrayList数组里面是获取的动态值              { 
                      XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName"); 
                      xesub4_3_1.InnerText = arrayPatches[z].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_1); 
                  
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb"); 
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_2);             } 
              } 
    请大侠们指教小弟,谢谢!!!
      

  5.   

    问题如下: 
    我的XML文件有2个节点即 PatchesName 和 PatchesKb ,我写的是2个循环(就是通过动态获取值,写入XML文件)。但是问题出现了如果在 arrayPatchesOffice数组有相应的值对应时显示出来的XML结果是没错的,如果arrayPatchesOffice没有相应的值时,则写入XML的值就变成重复的。我意思是想:当arrayPatchesOffice数组中的值都取完的情况之下,不写入该节点PatchesKb。只有arrayPatchesOffice数组中有值时才写入到XML 文件中,但是最后arrayPatches数组中的值要全部写入XML中。 
        for (int z = 0; z < arrayPatches.Count; z++) //arrayPatches外部ArrayList数组里面是获取的动态值          {                for (int j = 0; j < arrayPatchesOffice.Count;j++) //arrayPatchesOffice外部ArrayList数组里面是获取的动态值              { 
                      XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName"); 
                      xesub4_3_1.InnerText = arrayPatches[z].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_1); 
                  
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb"); 
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_2);             } 
              } 
    请大侠们指教小弟,谢谢!!!
      

  6.   

    应为取值的需要,外部是2个数组。就是要动态的从数组中取值写入XML文件
      

  7.   

    11楼如果是这样写的话,在XML中显示出来的效果就不是A B A B 的形式了,是AAA BBB的形式的。现在我需要得到的是第一种显示出来即A B A B 
      

  8.   

    for (int z = 0; z < arrayPatches.Count && z <arrayPatchesOffice.Count; z++) //arrayPatches外部ArrayList数组里面是获取的动态值          {                for (int j = 0; j < arrayPatchesOffice.Count;j++) //arrayPatchesOffice外部ArrayList数组里面是获取的动态值              { 
                      XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName"); 
                      xesub4_3_1.InnerText = arrayPatches[z].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_1); 
                  
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb"); 
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString(); 
                      xesub4_3.AppendChild(xesub4_3_2);             } 
              } 是不是这个意思??
      

  9.   

    我把源码给您看看:
    这是数组中取到的动态值:          for (int MendNumberPatches = 0; MendNumberPatches < strPatches.Count(); MendNumberPatches++)
              {
                  pRegKeyPatches_2 = pRegKeyPatches_1.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\" + strPatches[MendNumberPatches].ToString() + "\\Patches");
                  if (pRegKeyPatches_2 == null) 
                  {
                      continue;
                  }
                  String[] strPatches_1 = pRegKeyPatches_2.GetSubKeyNames();
                  if (strPatches_1.Count() == 0)
                  {
                      continue;
                  }
                  if(strPatches_1.Count() != 0)
                  {
                      for (int patchesNumber = 0; patchesNumber < strPatches_1.Count(); patchesNumber++)
                      {
                          pRegKeyPatches_4 = pRegKeyPatches_3.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\" + strPatches[MendNumberPatches].ToString() + "\\Patches\\" + strPatches_1[patchesNumber].ToString());
                          if (pRegKeyPatches_4 == null) 
                          {
                              continue;
                          }
                          if (pRegKeyPatches_4.GetValue("DisplayName") == null) 
                          {
                              continue;
                          }
                          Object name = pRegKeyPatches_4.GetValue("DisplayName").ToString() ;
                          String officeName = name.ToString();
                          int startIndex = officeName.LastIndexOf("(") + 1;
                          int length = officeName.Length - officeName.IndexOf(")");
                          if ((officeName.Length - length - startIndex) > 0) 
                          {
                              arrayPatchesOffice.Add(officeName.Substring(startIndex, officeName.Length - length - startIndex)).ToString(); // 字符串截取得到微软OFFICE补丁个数
                          }
                    
                          arrayPatches.Add(name).ToString();//所有补丁消息
                          
                      }
                  }           
              }
    这是写入XML文件代码:
      XmlElement xesub4_3 = xmlDoc.CreateElement("SoftwarePatch");
              for (int z = 0; z < arrayPatches.Count && z < arrayPatchesOffice.Count; z++) 
              {
                  for (int j = 0; j < arrayPatchesOffice.Count; j++) 
                  {
                     XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName");
                      xesub4_3_1.InnerText = arrayPatches[z].ToString();
                      xesub4_3.AppendChild(xesub4_3_1);
                   
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb");
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString();
                      xesub4_3.AppendChild(xesub4_3_2);
                      continue;
     
                 }
              }
              xesub4.AppendChild(xesub4_3);
    我需要在XML文件中显示为:
    PatchesName 
    PatchesKb
    PatchesName 
    PatchesKb 
    …………
    ……
    这样子的。这个数组arrayPatches中的数值是一个大集合包含数组arrayPatchesOffice中的值。但是如果arrayPatchesOffice数组中没有值的时候需不写入该节点PatchesKb 。写入的形式为A B A B的形式。
     
      

  10.   

    知道了,应该是这样子的:
            XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName");
              xesub4_3_1.InnerText = arrayPatches[z].ToString();
              xesub4_3.AppendChild(xesub4_3_1);
                  for (int j = z; j < arrayPatchesOffice.Count; j++) 
                  {
                    
                   
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb");
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString();
                      xesub4_3.AppendChild(xesub4_3_2);
                      break;
     
                 }
              }
    试试,看看是不是需要这样的结果!》?
      

  11.   

    写错了:
      for (int z = 0; z < arrayPatches.Count ; z++) 
              {         XmlElement xesub4_3_1 = xmlDoc.CreateElement("PatchesName");
              xesub4_3_1.InnerText = arrayPatches[z].ToString();
              xesub4_3.AppendChild(xesub4_3_1);
                  for (int j = z; j < arrayPatchesOffice.Count; j++) 
                  {
                    
                   
                      XmlElement xesub4_3_2 = xmlDoc.CreateElement("PatchesKb");
                      xesub4_3_2.InnerText = arrayPatchesOffice[j].ToString();
                      xesub4_3.AppendChild(xesub4_3_2);
                      break;
     
                 }
              }