C# 如何使用c读取各个属性值的所有xml节点#
我想执行一组不同的xml节点,这些节点由xml中各自的属性值标识。 但我面临的是,即使识别第二个属性值,也只执行set 1 xml节点。以下是我当前的代码:C# 如何使用c读取各个属性值的所有xml节点#,c#,xml,C#,Xml,我想执行一组不同的xml节点,这些节点由xml中各自的属性值标识。 但我面临的是,即使识别第二个属性值,也只执行set 1 xml节点。以下是我当前的代码: for (int m = 0; m < 10; m++) { attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value; foreach (string attr in attrVal_New.Split(',')) { Con
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中的属性标记名
,该名称位于我的xml中的'TestCondition'
下
这就是我在您的建议后所做的,我在选择第二个属性值时再次面临同样的问题,但是在状态下可用的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);
for(int m=0;m<10;m++)
{
XmlAttributeCollection coll=Update_Bugs.Item(m).Attributes;
字符串值=集合项(m).value;
attrVal\u New=Update\u Bugs[m]。属性[“TestCondition”]。值;
//m++;
foreach(attrVal_New.Split(',')中的字符串attr)
{
字符串attributelowercase=attr.ToLower();
//步骤1:创建bug
List BugWSResponseList1=新列表();
BugWSResponseList1=CreateBugs(文件路径\u EXPRESS\u API\u BugAdd\u CreateBugs\u数据文件,newValue);
输出:
ESG
Dr.Vatson
Blank
ESG
Dr.Hello
Blank
ESG
Dr.Vikram
Blank
ESG
Dr.Watson
Blank
我不知道为什么你有一个for循环,或者为什么你在属性值上有一个split,但是我会这样写代码
//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);
}
}
//获取所有的bug。
XmlNodeList Update_Bugs=XDoc.GetElementsByTagName(“Bugs”);
//循环浏览错误
foreach(更新中的XmlNode updateBug)
{
//检查测试条件
if(updateBug.Attributes[“TestCondition”]!=null)
{
//获取TestCondition的值。
字符串值=updateBug.Attributes[“TestCondition”].value;
字符串attributelowercase=value.ToLower();
控制台写入线(attributelowercase);
//获取节点的子节点。这将是节点。
XmlNodeList newValue=updateBug.ChildNodes;
//获取bug的xml字符串
字符串xmlString=updateBug.InnerXml;
Console.WriteLine(xmlString);
}
}
这将为您提供标签之间的TestCondition和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>
静电陀螺仪
沃森博士
空白
静电陀螺仪
沃森博士
空白
如何处理这一问题取决于您想用它做什么。显示用于获取更新bug
对象的类型和代码会很有用。我编辑了您的示例,删除了所有注释掉或未使用更新bug
的内容-看起来您没有发布应该使用更新的部分代码e_Bugs[m]
node…Update\u Bugs是XMLNode?@Alexei Levenkov请找到我的更新代码以及“Update\u Bugs”的代码片段@Vikram Bose是的没错..Update\u Bugs是根据xml中“TestCondition”下的属性值标记名识别的xml节点。请在使用变通方法后找到我的更新代码片段。我的Mis..请我正在使用更新的代码来尝试您的解决方法。我如何才能以xml文档而不是字符串的形式获得newvalue响应。请建议。
ESG
Dr.Vatson
Blank
ESG
Dr.Hello
Blank
ESG
Dr.Vikram
Blank
ESG
Dr.Watson
Blank
//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);
}
}
<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>