首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用c#读取所有xml节点的各个属性值

如何使用c#读取所有xml节点的各个属性值
EN

Stack Overflow用户
提问于 2013-09-14 15:06:51
回答 2查看 6.2K关注 0票数 3

我想执行不同的xml节点集,这些节点由xml中各自的属性值标识。但我面临的问题是,即使第二个属性值为identified.Here,我的当前代码也只能执行集合1的xml节点:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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”的代码片段:

代码语言:javascript
复制
 XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
                XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");

我使用这一部分来标识可在我的xml中的'TestCondition'下使用的Attribute Tag Names

这就是我在您的建议之后所做的事情,并且我再次面临相同的问题,因为我选择了第二个属性值,但是在STATE属性值下可用的xml节点集正在执行。

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

发布于 2013-09-14 15:45:07

代码语言:javascript
复制
    foreach (XmlElement xmlElement in nodeList)
            {
                foreach (XmlElement xmlElement1 in xmlElement.ChildNodes)
                {
                    foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)
                    {
                        string value = xmlElement2.InnerText;
                        Debug.WriteLine(value);
                    }
                }
            }

输出:

代码语言:javascript
复制
ESG
Dr.Vatson
Blank

ESG
Dr.Hello
Blank

ESG
Dr.Vikram
Blank

ESG
Dr.Watson
Blank
票数 1
EN

Stack Overflow用户

发布于 2013-09-14 18:06:11

我不知道为什么要使用for循环,也不知道为什么要对属性值进行拆分,但我会这样编写代码

代码语言:javascript
复制
//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作为输出,例如:

代码语言:javascript
复制
<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>

如何处理它取决于你想用它做什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18799289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档