2016-08-04 1 views
0

XML 파일은 아직 시작 단계입니다. 나는 XML과 디렉토리를 만들려면 나는이 기능을 가지고 : RunCommand가 comanda_cmd 첫 번째, 두 번째입니다 문자열XML 가져 오기 및 디렉토리 만들기 C#

private void ProcesNode(XmlNode node, string parentPath, string path, string first, string second, string BuiltUnit, string item) 
{ 

    if (!node.HasChildNodes || ((node.ChildNodes.Count == 2) && (node.FirstChild is System.Xml.XmlText))) 
    { 
     //MessageBox.Show(parentPath + "/" + node.Name); 
    } 
    else 
    { 
     foreach (XmlNode child in node.ChildNodes) 
     { 
      comanda_cmd = first + "/" + parentPath + second + "/" + parentPath + "/" + node.Name; 
      string status = RunCommand(comanda_cmd + "/project.pj /n"); 
      //content = "_GEN_PROJECT/" + ProjectName + "/" + BuiltUnit + "/" + item + "/" + parentPath + "/" + node.Name + " already exist"; 
      //MessageBox.Show(content); 
      //check_status(status, content); 
      ProcesNode(child, parentPath + "/" + node.Name, path, first, second, BuiltUnit, item); 
     } 
    } 
} 

을 실행하는 기능입니다

을 그리고 나는이 XML이 :

<unit> 
<Unit1> 
    <src> 
     <i> 
      <test1> 
       <test_in1> 
        <test_in_out> 
         <t> 
         </t> 
        </test_in_out> 
       </test_in1> 
      </test1> 
      <test2> 
       <test_in2> 
       </test_in2> 
      </test2> 
     </i> 
    </src> 
    <doc> 
     <i> 
      <test1> 
       <test_in1> 
        <test_in_out> 
         <t> 
         </t> 
        </test_in_out> 
       </test_in1> 
      </test1> 
      <test2> 
       <test_in2> 
       </test_in2> 
      </test2> 
     </i> 
    </doc> 
</Unit1> 
<Unit2> 
    <src> 
     <i> 
     </i> 
    </src> 
</Unit2> 

</unit> 

ProcesNode를 호출하고 디렉토리를 만듭니다. 예 : unit/Unit1/src/i/test1/test_in1/test_in_out 하지만 마지막 디렉토리 (내 경우 "t")는 생성되지 않습니다.

어디서 잘못 되었나요? 마지막 디렉토리를 만들지 않는 이유는 무엇입니까?

+0

가장 낮은 태그 ('')의 경우, 'if' 조건'(! node.HasChildren || ...)'는'true'이므로 디렉토리를 만드는'else' 블록을 입력하지 마십시오. –

+0

당신 말이 맞아요! 엄청난 감사를 보낸 Rene Vogt –

답변

0

가장 낮은 레벨의 태그 (예 : <t> 태그)에는 하위 노드가 없습니다. !node.HasChildNodes은 (아마도)이 디렉토리를 생성하는 true). So for the tags on the last level you never enter the else` 블록 때문에 if

if (!node.HasChildNodes || ((node.ChildNodes.Count == 2) && (node.FirstChild is System.Xml.XmlText))) 

Thatswhy

은 (true이다.

그것은 당신이 단순히 그 부분 (!node.HasChildNodes ||) 아웃 떠날 수 보인다. 자식 노드가없는 경우 foreach 루프는 입력되지 않습니다.

관련 문제