3

xml のそれぞれの属性値によって識別される xml ノードのさまざまなセットを実行したいと思います。しかし、私が直面しているのは、2番目の属性値が識別されていても、セット1のxmlノードのみが実行されているということです.Hereが私の現在のコードです:

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 として定義された異なる属性値があることに注意してください。このループを 2 回目に実行すると、属性値が検出されますが、属性値'STATUS'の下にある xml ノードが実行され'STATE'ます。提案してください。

「Update Bugs」のコード スニペットは次のとおりです。

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

私はこの部分を使用しAttribute Tag Namesて、xml で利用可能な xmlを識別しています'TestCondition'

これは私があなたの提案の後に行っていることであり、2 番目の属性値を取得しているため、同じ問題に再び直面していますが、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);
4

2 に答える 2

1
    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-14T07:45:07.093 に答える
0

なぜtouに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>

これを処理する方法は、何をしたいかによって異なります。

于 2013-09-14T10:06:10.677 に答える