繁体   English   中英

如何在Unity3D中使用C#在脚本中添加和删除XML文件中的节点?

[英]How to add and remove nodes in XML file from script using C# with Unity3D?

我需要在Unity3D中使用C#添加和删除XML节点。 在XML文件中,有Purchased-AssetsUnbought-Assets 单击按钮时,我想从“未Purchased-Assets Unbought-Assets节点中删除一个名为“ UnPurchased-Asset Purchased-Assets ,并将此项目作为“ Purchased-Assets添加到“ Purchased-Assets Purchased-Asset 我不知道从哪里开始。

到目前为止的代码(C#):

using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;

public class MyAppStuffCode : MonoBehaviour
{
    XmlDocument xml;
    public XmlNodeList _name;

    //Used to load XML file.
    xml = new XmlDocument();
    xml.Load(Application.dataPath + "/Resources/MyAppStuffXml.xml");

    // Use this for initialization
    void Start () 
    { 
    }

    // Update is called once per frame
    void Update () 
    {
    }

    public void OnButtonClicked(string BName)
    {
        // Code to add/remove XML nodes here!
    }
}

示例XML文件:

<TreasureChart>
    <Dudes>
        <Rapper>

            <!-- Purchased assets -->

            <Purchased-Assets>
                <Purchased-Asset>
                    <Name>BackTalk</Name>
                    <ID>A</ID>
                    <Points>20</Points>
                </Purchased-Asset>
                <Purchased-Asset>
                    <Name>Beard</Name>
                    <ID>B</ID>
                    <Points>20</Points>
                </Purchased-Asset>
                <Purchased-Asset>
                    <Name>IntroRap</Name>
                    <ID>C</ID>
                    <Points>20</Points>
                </Purchased-Asset>
                <Purchased-Asset>
                    <Name>Moustache</Name>
                    <ID>D</ID>
                    <Points>20</Points>
                </Purchased-Asset>
                <Purchased-Asset>
                    <Name>MyFaceDudeRap</Name>
                    <ID>E</ID>
                    <Points>20</Points>
                </Purchased-Asset>
                <Purchased-Asset>
                    <Name>MyFaceMyRap</Name>
                    <ID>F</ID>
                    <Points>20</Points>
                </Purchased-Asset>
            </Purchased-Assets>

            <!-- Unbought assets -->

            <Unbought-Assets>
                <UnPurchased-Asset>
                    <Name>Share</Name>
                    <ID>D</ID>
                    <Points>20</Points>
                </UnPurchased-Asset>
                <UnPurchased-Asset>
                    <Name>SunGlasses</Name>
                    <ID>E</ID>
                    <Points>20</Points>
                </UnPurchased-Asset>
            </Unbought-Assets>
        </Rapper>
    </Dudes>
</TreasureChart>
//Create a new XML element.
XmlElement node = xmlDocument.CreateElement("NewElement");

//Use node.AppendChild(child) to add more nodes to the node.

//Add the new element to the root of the document.
xmlDocument.DocumentElement.AppendChild(node);

//Remove the new element from the root of the document.
xmlDocument.DocumentElement.RemoveChild(node);

要在文档中查找特定节点,请使用索引器:

//Get the root node.
XmlElement root = xmlDocument.DocumentElement;

//Get the "Purchased-Assets" node that is nested inside the root.
XmlElement assets = root["Purchased-Assets"];

//Loop though each child
foreach(XmlNode childAsset in assets.ChildNodes)
{
     //Find the "ID" element of the child, you could easily replace this
     //to find the "Name" element.
     XmlElement id = childAsset["ID"];

     //If there is an "ID" element
     if(id != null)
     {
         //if the id node of the current child equals "20"
         if(id.InnerText.Equals("20"))
         {
              //then remove the asset from the "Purchased-Assets" node
              assets.RemoveChild(childAsset);
         }
     }
}

有关更多信息,请参见: MSDN:XmlDocument

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM