簡體   English   中英

如何將新節點添加到xml文件

[英]How to add new nodes to xml file

我正在嘗試將節點添加到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);
}

上面的代碼會將節點Descriptions添加到每個Student 如何將節點Descriptions添加到xml樹中更深的其他節點? 當前循環遍歷學生,但不遍歷例如: Grade

您需要遞歸添加節點,檢查代碼上的注釋以獲取更多詳細信息。

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);
        }
    }

如果僅需要1級更新,則可以使用嵌套的for循環,如下所示:

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,即使它超過一個級別,它也可以使您的節點沒有遞歸

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);
        }

此方法適用於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]);
        }
    }

像這樣在doWork方法中使用它

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM