我想执行不同的xml节点集,这些节点由xml中各自的属性值标识。但我面临的问题是,即使第二个属性值为identified.Here,我的当前代码也只能执行集合1的xml节点:
for (int m = 0; m < 10; m++)
{
attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
foreach (string attr in attrVal_New.Split(','))
{
Console.WriteLine(attr);
....请按如下方式查找示例xml:
<DrWatson>
<Bugs Name="Testing 11" TestCondition="STATE">
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
</Bugs>
<Bugs Name="Testing 22" TestCondition="STATUS">
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
</Bugs>
</DrWatson>请注意,在TestCondition下有不同的属性值定义为'STATE‘和STATUS。第二次运行此循环时,属性值被检测为'STATUS',但它会执行'STATE' attribute value.Please suggest下的xml节点。
下面是“Update Bugs”的代码片段:
XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");我使用这一部分来标识可在我的xml中的'TestCondition'下使用的Attribute Tag Names。
这就是我在您的建议之后所做的事情,并且我再次面临相同的问题,因为我选择了第二个属性值,但是在STATE属性值下可用的xml节点集正在执行。
for (int m = 0; m < 10; m++)
{
XmlAttributeCollection coll = Update_Bugs.Item(m).Attributes;
string value = coll.Item(m).Value;
attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
//m++;
foreach (string attr in attrVal_New.Split(','))
{
string attributelowercase = attr.ToLower();
//Step1: Create Bugs
List<string> BugWSResponseList1 = new List<string>();
BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile, newValue);发布于 2013-09-14 15:45:07
foreach (XmlElement xmlElement in nodeList)
{
foreach (XmlElement xmlElement1 in xmlElement.ChildNodes)
{
foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)
{
string value = xmlElement2.InnerText;
Debug.WriteLine(value);
}
}
}输出:
ESG
Dr.Vatson
Blank
ESG
Dr.Hello
Blank
ESG
Dr.Vikram
Blank
ESG
Dr.Watson
Blank发布于 2013-09-14 18:06:11
我不知道为什么要使用for循环,也不知道为什么要对属性值进行拆分,但我会这样编写代码
//Get all the bugs.
XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");
//Loop through the bugs
foreach(XmlNode updateBug in Update_Bugs)
{
//Check for the test condition
if (updateBug.Attributes["TestCondition"] != null)
{
//Get the value of TestCondition.
string value = updateBug.Attributes["TestCondition"].Value;
string attributelowercase = value.ToLower();
Console.WriteLine(attributelowercase);
//Get the children of the node. This will be the <Bug> nodes.
XmlNodeList newValue = updateBug.ChildNodes;
//Get the xml string of the bugs
string xmlString = updateBug.InnerXml;
Console.WriteLine(xmlString);
}
}这将为您提供标记之间的TestCondotion和XML作为输出,例如:
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>如何处理它取决于你想用它做什么。
https://stackoverflow.com/questions/18799289
复制相似问题