c# - 如何使用 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(','))
    {
        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 下定义了不同的属性值,如“状态”和“状态”。第二次运行此循环时,属性值被检测为 'STATUS' 但它会执行 'STATE' 属性值下存在的 xml 节点。请提出建议。

这是“更新错误”的代码片段,如下所示:

 XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
                XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");

我正在使用这部分来识别 xml 中 'TestCondition' 下可用的 xml 中的 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);

最佳答案

    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

关于c# - 如何使用 C# 读取各个属性值的所有 xml 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799289/

相关文章:

XML::Twig purge 在 perl 程序中不释放系统内存

c# - MVC 从 ReturnURL 获取 ID

c# - 相同对象的数组

c# - 通用列表,使用条件语句计数的项目

c# - 未找到结果时的错误页面

c# - 在 Microsoft SQL Server 中一次对大量 XML 文件使用 Convert Style Option 2

python - 通过python爬取sitemap.xml

XML验证: 'No matching global declaration available for the validation root'

xml - 我用什么将 CSV 数据显示为图表?

c# - 命名空间 "X"中不存在名称 "using:Y"