2017-01-22 29 views
0

ノードをxmlファイルに追加しようとしています。xmlファイルに新しいノードを追加する方法

XMLファイル:

<Students> 
<Student> 
    <Address> ... </Address> 
    <Grade> ... </Grade> 
</Student> 
    ... 
</Students> 

ここでは、私が行って何です:

public XmlElement createNode(XmlDocument xmlDoc) 
{ 
    XmlElement trElement = xmlDoc.CreateElement("Descriptions"); 
    XmlElement textElement = xmlDoc.CreateElement("Text"); 
    textElement.SetAttribute("String", "Abcdef"); 
    textElement.SetAttribute("Language", "ENG"); 
    trElement.AppendChild(textElement); 
    return trElement; 
} 
public void doWork(string filePath) 
{ 
    XmlDocument fromXML; 
    fromXML = new XmlDocument(); 
    fromXML.Load(filePath); 
    XmlNode fromRoot = fromXML.DocumentElement; 
    foreach (XmlNode node in fromRoot.ChildNodes) 
    { 
     if (node.ChildNodes[0].Name != "Descriptions") 
     { 
      var trElement = createNode(fromXML); 
      node.InsertBefore(trElement, node.ChildNodes[0]); 
     } 
    } 
    fromXML.Save(Console.Out); 
} 

コードは、上記の各StudentにノードDescriptionsを追加します。どのようにノードDescriptionsをxmlツリーのより深いところにある他のノードに追加できますか?現在のループは生徒に対して繰り返し実行されますが、例:Gradeなどではありません。

+0

ようdoWork方法でそれを使用するためにこの方法が機能するには、ループの入れ子になったが、この例のためのタスクを実行しないのだろうか? –

+0

私に例を挙げてもらえますか? – Sam

+0

ここで再帰が必要かもしれないと思います。 –

答えて

1

ノードを再帰的に追加する必要があります。詳細はコードのコメントを確認してください。

public static XmlNode CreateNode(XmlDocument document) 
    { 
     XmlElement trElement = document.CreateElement("Descriptions"); 
     XmlElement textElement = document.CreateElement("Text"); 
     textElement.SetAttribute("String", "Abcdef"); 
     textElement.SetAttribute("Language", "ENG"); 
     trElement.AppendChild(textElement); 
     return trElement; 
    } 

    public static void doWork(string filePath) 
    { 
     XmlDocument fromXML; 
     fromXML = new XmlDocument(); 
     fromXML.Load(filePath); 
     XmlNode fromRoot = fromXML.DocumentElement; 
     // Start from <Student></Student> 
     foreach (XmlNode node in fromRoot.ChildNodes) 
     { 
      InsertNewNodes(node, fromXML); 
     } 
     fromXML.Save(Console.Out); 
    } 

    public static void InsertNewNodes(XmlNode root, XmlDocument document) 
    { 
     var hasDescription = false; 

     // Iterate over every first level child looking for "Descriptions" 
     foreach (XmlNode node in root.ChildNodes) 
     { 
      if (node.Name == "Descriptions") 
      { 
       hasDescription = true; 
      } 
      // recursively call InsertNewNodes 
      if (node.ChildNodes.Count > 0) 
      { 
       InsertNewNodes(node, document); 
      } 
     } 
     // Adjust the root.LastChild.NodeType criteria to only add the nodes when you want 
     // In this case I only add the Description if the subnode has Elements 
     if (!hasDescription && root.LastChild.NodeType == XmlNodeType.Element) 
     { 
      var newNode = CreateNode(document); 
      root.PrependChild(newNode); 
     } 
    } 
0

あなただけのだけにしてupdationの1つのレベルを必要とする場合、あなたはこのようなループのために入れ子に使用することができます。

public void doWork(string filePath) 
{ 
    XmlDocument fromXML; 
    fromXML = new XmlDocument(); 
    fromXML.Load(filePath); 
    XmlNode fromRoot = fromXML.DocumentElement; 
    foreach (XmlNode node in fromRoot.ChildNodes) 
    { 
     foreach (XmlNode childNode in node) { 
      if (childNode.Name == "Grade") 
      { 
       if (childNode.ChildNodes[0].Name != "Descriptions") 
       { 
        var trElement = createNode(fromXML); 
        childNode.InsertBefore(trElement, childNode.ChildNodes[0]); 

       } 
      } 
     } 

    } 
    fromXML.Save(Console.Out); 
} 

が、より良い方法は、あなたがしても出て再帰を持つノードを取得しますれたXpathを使用するようになります1つ以上のレベルです。

public void doWork(string filePath) 
     { 
      XmlDocument fromXML; 
      fromXML = new XmlDocument(); 
      fromXML.Load(filePath); 
      XmlNode fromRoot = fromXML.DocumentElement; 
      foreach (XmlNode node in fromRoot.SelectNodes("//Grade")) 
      { 
       if (node.ChildNodes[0].Name != "Descriptions") 
       { 
        var trElement = createNode(fromXML); 
        node.InsertBefore(trElement, node.ChildNodes[0]); 

       } 

      } 
      fromXML.Save(Console.Out); 
     } 
+0

あなたのソリューションをありがとう、本当に助けになりました。 – Sam

0

Nレベルの数

private void HandleNode(XmlNode node, XmlDocument xmlDoc) 
    { 
     if (node.HasChildNodes) 
     { 
      foreach (XmlNode child in node.ChildNodes) 
      { 
       if (node.ChildNodes[0].Name != "Descriptions" && node.Name != "Descriptions") 
       { 
        var trElement = createNode(xmlDoc); 
        node.InsertBefore(trElement, node.ChildNodes[0]); 
       } 
       if (node.Name != "Descriptions") 
        HandleNode(child, xmlDoc); 
      } 
     } 
     else 
     { 
      var trElement = createNode(xmlDoc); 
      node.InsertBefore(trElement, node.ChildNodes[0]); 
     } 
    } 

その

public void doWork(string filePath) 
    { 
     XmlDocument fromXML; 
     fromXML = new XmlDocument(); 
     fromXML.Load(filePath); 
     XmlNode fromRoot = fromXML.DocumentElement; 
     foreach (XmlNode node in fromRoot.ChildNodes) 
     { 
      HandleNode(node, fromXML); 
     } 
     fromXML.Save(filePath); 
    } 
関連する問題