问题如下:
我的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);
}
}
请大侠们指教小弟,谢谢!!!
我的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);
}
}
请大侠们指教小弟,谢谢!!!
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); }
}
我的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); }
}
请大侠们指教小弟,谢谢!!!
我的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); }
}
请大侠们指教小弟,谢谢!!!
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); }
} 是不是这个意思??
这是数组中取到的动态值: 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的形式。
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;
}
}
试试,看看是不是需要这样的结果!》?
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;
}
}